import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__pm_index_mjs__ from "../../pm/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ 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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
SettingMenuContext.displayName = 'SettingMenuContext';
const SettingCenterProvider = (props)=>{
    const configureItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__pm_index_mjs__.SettingsCenterContext);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SettingMenuContext.Provider, {
        value: configureItems,
        children: props.children
    });
};
const SettingsCenterConfigure = ()=>{
    const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const settings = app.systemSettingsManager.getList(false);
    const allAclSnippets = app.systemSettingsManager.getAclSnippets();
    const [snippets, setSnippets] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const allChecked = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>snippets.includes('pm.*') && snippets.every((item)=>!item.startsWith('!pm.')), [
        snippets
    ]);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { loading, refresh } = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                        checked: checked,
                        onChange: ()=>handleChange(checked, record)
                    });
                }
            }
        ],
        dataSource: settings.filter((v)=>false !== v.isTopLevel).map((v)=>{
            if (false !== v.showTabs) return v;
            return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.omit)(v, 'children');
        })
    });
};
export { SettingCenterProvider, SettingsCenterConfigure };
