import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import functionResourcesMap from 'doom-@permission-functionResourcesMap';
import roleTemplatesMap from 'doom-@permission-roleTemplatesMap';
import { intersection, sortBy } from 'es-toolkit';
import { Fragment, useMemo } from 'react';
import { useLang } from 'rspress/runtime';
import { useTranslation } from '../hooks/index.js';
import { X } from './_X.js';
export const VerbsMap = {
    view: ['get', 'list', 'watch'],
    create: ['create'],
    update: ['update', 'patch'],
    delete: ['delete', 'deletecollection'],
};
export const Verbs = Object.keys(VerbsMap);
const FUNCTION_DISPLAY_NAME_ZH = 'cpaas.io/functionresource.function.display-name';
const FUNCTION_DISPLAY_NAME_EN = 'cpaas.io/functionresource.function.display-name.en';
const DISPLAY_NAME_ZH = 'cpaas.io/display-name';
const DISPLAY_NAME_EN = 'cpaas.io/display-name.en';
const TEXT_CENTER_STYLE = { textAlign: 'center' };
const TEXT_NO_WRAP_STYLE = { whiteSpace: 'nowrap' };
const RolesPermission = ({ functionResource, roleTemplates, verb, }) => {
    const functionResourceName = functionResource.metadata.name;
    const actions = functionResource.metadata.annotations['auth.cpaas.io/actions'];
    return roleTemplates.map(({ metadata: { name }, spec: { rules } }) => {
        const found = rules.find((r) => ['*', functionResourceName].includes(r.functionResourceRef));
        let hasPermission = false;
        if (found) {
            let verbs;
            if (actions) {
                const actionsVerbs = actions.split(',');
                if (found.verbs.includes('*')) {
                    verbs = actionsVerbs;
                }
                else {
                    verbs = intersection(found.verbs, actionsVerbs);
                }
            }
            else {
                verbs = found.verbs;
            }
            if (verbs.includes('*') ||
                VerbsMap[verb].every((v) => verbs.includes(v))) {
                hasPermission = true;
            }
        }
        return (_jsx(X.td, { style: TEXT_CENTER_STYLE, children: hasPermission ? '✓' : '✕' }, name));
    });
};
const allFunctionResources = Object.values(functionResourcesMap).reduce((acc, curr) => Object.assign(acc, ...curr.map((fr) => ({ [fr.metadata.name]: fr }))), {});
export const K8sPermissionTable = ({ functions }) => {
    const functionResources = useMemo(() => functions.flatMap((name) => {
        const matched = allFunctionResources[name];
        if (!matched) {
            console.error(`FunctionResource \`${name}\` not found!\n`);
            return [];
        }
        return matched;
    }), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    functions);
    const roleTemplates = useMemo(() => sortBy(Object.values(roleTemplatesMap).flat(), [
        (it) => it.metadata.annotations['auth.cpaas.io/role.id'],
    ]), []);
    const t = useTranslation();
    const lang = useLang();
    const isZh = lang === 'zh';
    return (_jsxs(X.table, { children: [_jsx("thead", { children: _jsxs(X.tr, { children: [_jsx(X.th, { children: t('function') }), _jsx(X.th, { style: TEXT_NO_WRAP_STYLE, children: t('action') }), roleTemplates.map(({ metadata: { annotations, name } }) => (_jsx(X.th, { children: isZh
                                ? annotations[DISPLAY_NAME_ZH]
                                : annotations[DISPLAY_NAME_EN] }, name)))] }) }), _jsx("tbody", { children: functionResources.map((fr) => {
                    const { metadata: { annotations, name }, } = fr;
                    return (_jsxs(Fragment, { children: [_jsxs(X.tr, { children: [_jsxs(X.td, { rowSpan: 4, style: TEXT_CENTER_STYLE, children: [isZh
                                                ? annotations[FUNCTION_DISPLAY_NAME_ZH]
                                                : annotations[FUNCTION_DISPLAY_NAME_EN], _jsx("br", {}), _jsx("code", { children: name })] }), _jsx(X.td, { children: t('view') }), _jsx(RolesPermission, { functionResource: fr, roleTemplates: roleTemplates, verb: "view" })] }), Verbs.slice(1).map((verb) => (_jsxs(X.tr, { children: [_jsx(X.td, { children: t(verb) }), _jsx(RolesPermission, { functionResource: fr, roleTemplates: roleTemplates, verb: verb })] }, verb)))] }, name));
                }) })] }));
};
export default K8sPermissionTable;
