import { usePermissionStore } from '@/stores/permission'

// 权限指令
const permission = {
  mounted(el, binding) {
    const { value } = binding
    const permissionStore = usePermissionStore()
    
    if (value) {
      const hasPermission = permissionStore.hasPermission.value(value)
      if (!hasPermission) {
        el.style.display = 'none'
      }
    }
  },
  
  updated(el, binding) {
    const { value } = binding
    const permissionStore = usePermissionStore()
    
    if (value) {
      const hasPermission = permissionStore.hasPermission.value(value)
      if (!hasPermission) {
        el.style.display = 'none'
      } else {
        el.style.display = ''
      }
    }
  }
}

// 角色指令
const role = {
  mounted(el, binding) {
    const { value } = binding
    const permissionStore = usePermissionStore()
    
    if (value) {
      const hasRole = permissionStore.hasRole.value(value)
      if (!hasRole) {
        el.style.display = 'none'
      }
    }
  },
  
  updated(el, binding) {
    const { value } = binding
    const permissionStore = usePermissionStore()
    
    if (value) {
      const hasRole = permissionStore.hasRole.value(value)
      if (!hasRole) {
        el.style.display = 'none'
      } else {
        el.style.display = ''
      }
    }
  }
}

export default {
  install(app) {
    app.directive('permission', permission)
    app.directive('role', role)
  }
}