'use client';
import { LockClosedIcon } from '@heroicons/react/24/solid';
import { PHXCard } from '../Card';
import React, { useEffect, useState } from 'react';
import { PHXClientQueryV3 } from '../Func/GRPC/PHXGrpcClientV3';
import { GET_PERMISSION_DETAIL_USER_V3 } from '../../query';
import PHXFuncGetLoggedInfo from '../Func/getLoginInfo';
export function PHXPagePermissionV3({ children, code }) {
    const [perCode, setPerCode] = useState([]);
    const userSessionInfo = PHXFuncGetLoggedInfo();
    useEffect(() => {
        if (userSessionInfo) {
            const { id, school_id } = userSessionInfo;
            const fetchDataPer = async () => {
                var _a, _b;
                try {
                    const res = await PHXClientQueryV3({
                        query: 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.createElement(React.Fragment, null, children);
        }
        else {
            return (React.createElement("div", { className: 'mt-5' },
                React.createElement(PHXCard, null,
                    React.createElement("div", { className: 'mb-10 mt-10 sm:mb-20 sm:mt-20 lg:mb-32 lg:mt-32' },
                        React.createElement("div", { className: 'flex items-center justify-center' },
                            React.createElement(LockClosedIcon, { className: 'text-gray-400', height: 70, width: 70 })),
                        React.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