import { store } from '../store/index';

function checkFunPermission(el, binding) {
	try {
		const { value } = binding;

		// 当为1时则标识是体验用户，拥有全部能力
		const userInfos = store.state.userInfos.userInfos;
		const clientIsEnterpriseMode = userInfos.client.clientIsEnterpriseMode;
		const clientCode = userInfos.client.code;

		if (clientIsEnterpriseMode !== 1) {
			const permissions = store.state.funPerission[value];

			if (!permissions || !Array.isArray(permissions)) {
				throw new Error(`Invalid permissions for value: ${value}`);
			}

			const hasPermission = permissions.includes(clientCode);
			console.log(hasPermission)
			if (!hasPermission) {
				el.parentNode && el.parentNode.removeChild(el);
			}
		}
	} catch (error) {
		console.error('v-funperission error:', error);
	}
}

export default {
	// 在绑定元素的父组件被挂载后调用
	mounted(el, binding) {
		checkFunPermission(el, binding);
	},
	// 在包含组件的 VNode 及其子组件的 VNode 更新后调用
	updated(el, binding) {
		checkFunPermission(el, binding);
	},
};
