import type { Directive, DirectiveBinding } from 'vue'
import { userStore } from '@/stores/user'

export const vAuth: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    el.addEventListener('click', async (e: Event) => {
      e.stopPropagation()

      const user = userStore();
      const callback = binding.value

      if (typeof callback !== 'function') {
        return
      }

      if (!user.checkUserLoggedIn) {
        // 这里调用你现有的显示登录框方法
        user.showLoginModel = true
        return
      }

      try {
        await callback(e)
      } catch (error) {
        console.error('操作执行失败:', error)
      }
    })
  }
}

export default {
  install(app: any) {
    app.directive('auth', vAuth)
  }
}
