/*
 * @Author       : xh
 * @Date         : 2022-12-05 10:04:57
 * @LastEditors: y001554
 * @FileName     :
 */
// import store from '@/store'

function checkPermission(el, binding) {
  const { value } = binding
  const userBtnList = []

  let hasPermission
  if (value === 'none') {
    hasPermission = true
  } else if (typeof value === 'string') {
    // hasPermission = userBtnList.some((item, index) => {
    //   return item.component === value
    // })
    hasPermission = userBtnList.includes(value)
  } else if (value instanceof Array) {
    value.forEach(() => {
      hasPermission = userBtnList.includes(value)
      // hasPermission = userBtnList.some((item) => {
      //   return item.component === v
      // })
    })
  } else if (typeof value === 'function') {
    const res = value()
    hasPermission = userBtnList.includes(res)
    // hasPermission = userBtnList.some((item, index) => {

    //   return item.component === res
    // })
    console.log('res', typeof value, res, hasPermission)
  } else {
    throw new Error(`格式错误，应为：v-permission="'aaa'" 或 v-permission="['aaa','bbb']"或 v-permission="function"`)
  }
  if (!hasPermission) {
    el.style.display = 'none'
  } else {
    el.style.display = 'inline-block'
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding)
  },
  update(el, binding) {
    checkPermission(el, binding)
  }
}
