"use strict";
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.PHXPagePermissionV3 = PHXPagePermissionV3;
const tslib_1 = require("tslib");
const solid_1 = require("@heroicons/react/24/solid");
const Card_1 = require("../Card");
const react_1 = tslib_1.__importStar(require("react"));
const PHXGrpcClientV3_1 = require("../Func/GRPC/PHXGrpcClientV3");
const query_1 = require("../../query");
const getLoginInfo_1 = tslib_1.__importDefault(require("../Func/getLoginInfo"));
function PHXPagePermissionV3({ children, code }) {
    const [perCode, setPerCode] = (0, react_1.useState)([]);
    const userSessionInfo = (0, getLoginInfo_1.default)();
    (0, react_1.useEffect)(() => {
        if (userSessionInfo) {
            const { id, school_id } = userSessionInfo;
            const fetchDataPer = async () => {
                var _a, _b;
                try {
                    const res = await (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
                        query: query_1.GET_PERMISSION_DETAIL_USER_V3,
                        variables: { id: id, school_id },
                    });
                    const userPermission = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.permission_user;
                    const rolePermission = (_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.permission_role;
                    const dataCodePermissionRole = rolePermission === null || rolePermission === void 0 ? void 0 : rolePermission.map((item) => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.permission) === null || _a === void 0 ? void 0 : _a.code; });
                    const dataCodePermissionUser = userPermission === null || userPermission === void 0 ? void 0 : userPermission.map((item) => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.permission) === null || _a === void 0 ? void 0 : _a.code; });
                    const allCodes = [...dataCodePermissionRole, ...dataCodePermissionUser];
                    const uniqueValues = Array.from(new Set(allCodes));
                    setPerCode(uniqueValues);
                }
                catch (error) {
                    console.error('Error fetching school data:', error);
                }
            };
            fetchDataPer().catch(console.error);
        }
    }, []);
    if (perCode.length > 0) {
        const checkPermission = perCode.some((item) => item === code);
        if (checkPermission) {
            return react_1.default.createElement(react_1.default.Fragment, null, children);
        }
        else {
            return (react_1.default.createElement("div", { className: 'mt-5' },
                react_1.default.createElement(Card_1.PHXCard, null,
                    react_1.default.createElement("div", { className: 'mb-10 mt-10 sm:mb-20 sm:mt-20 lg:mb-32 lg:mt-32' },
                        react_1.default.createElement("div", { className: 'flex items-center justify-center' },
                            react_1.default.createElement(solid_1.LockClosedIcon, { className: 'text-gray-400', height: 70, width: 70 })),
                        react_1.default.createElement("div", { className: 'mt-5 flex items-center justify-center text-base  font-bold text-gray-800' }, "B\u1EA1n kh\u00F4ng c\u00F3 quy\u1EC1n truy c\u1EADp")))));
        }
    }
    return null;
}
//# sourceMappingURL=PagePermissionV3.js.map