import store from '@/store'

function checkAuth(el, binding) {
  const { value: { fieldName, fieldValue }} = binding
  const authAccountInfo = (store.getters && store.getters?.authAccountInfo) || {}

  const authRoles = fieldValue || []
  if (authRoles.length > 0) {
    const hasAuth = (authAccountInfo[fieldName] || []).some((role) => {
      return authRoles.includes(role)
    })

    if (!hasAuth) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    if ((authAccountInfo[fieldName] || []).length == 0) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

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