import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { createContext, useContext, useMemo, useState } from "react";
import { App, Checkbox, Table } from "antd";
import { omit } from "lodash";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../../api-client/index.mjs";
import { useApp } from "../../../application/index.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
import { useCompile } from "../../../schema-component/index.mjs";
import { SettingsCenterContext } from "../../pm/index.mjs";
import { useStyles } from "../style.mjs";
const getChildrenKeys = function() {
    let data = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], arr = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
    for (const item of data){
        arr.push(item.aclSnippet);
        if (item.children && item.children.length) getChildrenKeys(item.children, arr);
    }
    return arr;
};
const SettingMenuContext = /*#__PURE__*/ createContext(null);
SettingMenuContext.displayName = 'SettingMenuContext';
const SettingCenterProvider = (props)=>{
    const configureItems = useContext(SettingsCenterContext);
    return /*#__PURE__*/ jsx(SettingMenuContext.Provider, {
        value: configureItems,
        children: props.children
    });
};
const SettingsCenterConfigure = ()=>{
    const app = useApp();
    const { message } = App.useApp();
    const { styles } = useStyles();
    const record = useRecord();
    const api = useAPIClient();
    const compile = useCompile();
    const settings = app.systemSettingsManager.getList(false);
    const allAclSnippets = app.systemSettingsManager.getAclSnippets();
    const [snippets, setSnippets] = useState([]);
    const allChecked = useMemo(()=>snippets.includes('pm.*') && snippets.every((item)=>!item.startsWith('!pm.')), [
        snippets
    ]);
    const { t } = useTranslation();
    const { loading, refresh } = useRequest({
        resource: 'roles.snippets',
        resourceOf: record.name,
        action: 'list',
        params: {
            paginate: false
        }
    }, {
        onSuccess (data) {
            setSnippets((null == data ? void 0 : data.data) || []);
        }
    });
    const resource = api.resource('roles.snippets', record.name);
    const handleChange = async (checked, record)=>{
        const childrenKeys = getChildrenKeys(null == record ? void 0 : record.children, []);
        const totalKeys = childrenKeys.concat(record.aclSnippet);
        if (checked) {
            await resource.add({
                values: totalKeys.map((v)=>'!' + v)
            });
            refresh();
        } else {
            await resource.remove({
                values: totalKeys.map((v)=>'!' + v)
            });
            refresh();
        }
        message.success(t('Saved successfully'));
    };
    return /*#__PURE__*/ jsx(Table, {
        className: styles,
        loading: loading,
        rowKey: 'key',
        pagination: false,
        expandable: {
            defaultExpandAllRows: true
        },
        columns: [
            {
                dataIndex: 'title',
                title: t('Plugin name'),
                render: (value)=>compile(value)
            },
            {
                dataIndex: 'accessible',
                title: /*#__PURE__*/ jsxs(Fragment, {
                    children: [
                        /*#__PURE__*/ jsx(Checkbox, {
                            checked: allChecked,
                            onChange: async ()=>{
                                const values = allAclSnippets.map((v)=>'!' + v);
                                if (allChecked) await resource.add({
                                    values
                                });
                                else await resource.remove({
                                    values
                                });
                                refresh();
                                message.success(t('Saved successfully'));
                            }
                        }),
                        ' ',
                        t('Accessible')
                    ]
                }),
                render: (_, record)=>{
                    const checked = !snippets.includes('!' + record.aclSnippet);
                    return /*#__PURE__*/ jsx(Checkbox, {
                        checked: checked,
                        onChange: ()=>handleChange(checked, record)
                    });
                }
            }
        ],
        dataSource: settings.filter((v)=>false !== v.isTopLevel).map((v)=>{
            if (false !== v.showTabs) return v;
            return omit(v, 'children');
        })
    });
};
export { SettingCenterProvider, SettingsCenterConfigure };
