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__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_uniq__ from "lodash/uniq";
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__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PermisionProvider_mjs__ from "./PermisionProvider.mjs";
const SnippetCheckboxGroup = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox.Group, {
        style: {
            width: '100%',
            display: 'block'
        },
        value: props.value,
        onChange: (values)=>{
            const snippets = [
                'ui.*',
                'pm',
                'pm.*',
                'app'
            ];
            const disallowSnippets = snippets.map((key)=>"!".concat(key));
            const value = (0, __WEBPACK_EXTERNAL_MODULE_lodash_uniq__["default"])([
                ...props.value || [],
                ...values
            ]).filter((key)=>key && !disallowSnippets.includes(key)).map((key)=>{
                if (!snippets.includes(key) || (null == values ? void 0 : values.includes(key))) return key;
                return "!".concat(key);
            });
            for (const key of snippets)if (!value.includes(key) && !value.includes("!".concat(key))) value.push("!".concat(key));
            props.onChange(value);
        },
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    marginTop: 16
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                    value: "ui.*",
                    children: t('Allows to configure interface')
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                    value: "pm",
                    children: t('Allows to install, activate, disable plugins')
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                    value: "pm.*",
                    children: t('Allows to configure plugins')
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Checkbox, {
                    value: "app",
                    children: t('Allows to clear cache, reboot application')
                })
            })
        ]
    });
});
const RoleConfigure = ()=>{
    const { update, currentRecord } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__PermisionProvider_mjs__.PermissionContext);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
        components: {
            SnippetCheckboxGroup
        },
        schema: {
            type: 'void',
            name: 'form',
            'x-component': 'Form',
            'x-component-props': {
                useValues: (options)=>{
                    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
                    return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>api.resource('roles').get({
                            filterByTk: currentRecord.name
                        }).then((res)=>{
                            var _res_data, _record_snippets;
                            const record = null == res ? void 0 : null === (_res_data = res.data) || void 0 === _res_data ? void 0 : _res_data.data;
                            null === (_record_snippets = record.snippets) || void 0 === _record_snippets || _record_snippets.forEach((key)=>{
                                record[key] = true;
                            });
                            return {
                                data: record
                            };
                        }), options);
                },
                effects () {
                    (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.onFieldChange)('*', async (field, form)=>{
                        if (!form.modified) return;
                        await update(field, form);
                    });
                }
            },
            properties: {
                snippets: {
                    title: t('Configure permissions'),
                    type: 'boolean',
                    'x-decorator': 'FormItem',
                    'x-component': 'SnippetCheckboxGroup'
                },
                allowNewMenu: {
                    title: t('Menu permissions'),
                    'x-decorator': 'FormItem',
                    'x-component': 'Checkbox',
                    'x-content': t('New menu items are allowed to be accessed by default.')
                }
            }
        }
    });
};
export { RoleConfigure };
