/**
 * v-permissions
 * 按钮权限指令
 */
import type { Directive, DirectiveBinding } from 'vue'
import { useUserStore } from '@/stores/modules/user'

const permissions: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding
    if (value === undefined) return

    const userStore = useUserStore()
    if (userStore.superAdmin) return

    const permissions = userStore.permissions

    if (value instanceof Array) {
      for (let item of value) {
        if (permissions.includes(item)) {
          return
        }
      }
    } else {
      if (permissions.includes(value)) {
        return
      }
    }
    el.remove()
  }
}

export default permissions
