export default function (app) {
  app.directive('preReClick', {
    mounted(el, binding) {
      let delay = 2000

      if (typeof binding.value === 'number' && binding.value > 0) {
        delay = binding.value
      } else {
        console.warn(
          'Invalid delay value for preReClick directive. Using default value.'
        )
      }

      // 定义点击事件处理函数
      const clickHandler = function () {
        if (!el.disabled) {
          el.disabled = true
          el.style.pointerEvents = 'none' // 禁止点击
          el.style.opacity = '0.6' // 视觉上变灰

          setTimeout(() => {
            el.disabled = false
            el.style.pointerEvents = '' // 恢复点击
            el.style.opacity = '' // 恢复样式
          }, delay)
        }
      }

      el.__clickHandler = clickHandler
      el.addEventListener('click', clickHandler)
    },
    unmounted(el) {
      el.removeEventListener('click', el.__clickHandler)
      delete el.__clickHandler
    }
  })
}
