/**
 * 自定义权限按钮指令
 * 使用方法 v-permission="'companyAdmin:add'"
 * @param {*} store 权限数组
 * @returns 
 */

import { lookPermissionUser } from '@/config'


export default function createPermissionDirective(store) {
    function getPermissionCodes(store) {
        const userId = store.state.user.info?.userId || ''
        // 写死判断用户ID是 888 的用户隐藏掉所有操作
        if (lookPermissionUser.includes(userId)) {
            return []
        } else {
            return ['permission']
        }
        /* const all = store.state.user.info?.permissionList || []
        return all
            .filter(item => item?.type === 'permission' && item?.permissionCode)
            .map(item => item.permissionCode) */
    }

    return {
        mounted(el, binding) {
            const requiredCode = binding.value
            const permissionCodes = getPermissionCodes(store)

            if (!permissionCodes.includes(requiredCode)) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        }
    }
}
