import { jsx, jsxs } from "react/jsx-runtime";
import { useContext } from "react";
import { connect, onFieldChange } from "@tachybase/schema";
import { Checkbox } from "antd";
import uniq from "lodash/uniq";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../../api-client/index.mjs";
import { SchemaComponent } from "../../../schema-component/index.mjs";
import { PermissionContext } from "./PermisionProvider.mjs";
const SnippetCheckboxGroup = connect((props)=>{
    const { t } = useTranslation();
    return /*#__PURE__*/ jsxs(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 = uniq([
                ...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__*/ jsx("div", {
                style: {
                    marginTop: 16
                },
                children: /*#__PURE__*/ jsx(Checkbox, {
                    value: "ui.*",
                    children: t('Allows to configure interface')
                })
            }),
            /*#__PURE__*/ jsx("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ jsx(Checkbox, {
                    value: "pm",
                    children: t('Allows to install, activate, disable plugins')
                })
            }),
            /*#__PURE__*/ jsx("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ jsx(Checkbox, {
                    value: "pm.*",
                    children: t('Allows to configure plugins')
                })
            }),
            /*#__PURE__*/ jsx("div", {
                style: {
                    marginTop: 8
                },
                children: /*#__PURE__*/ jsx(Checkbox, {
                    value: "app",
                    children: t('Allows to clear cache, reboot application')
                })
            })
        ]
    });
});
const RoleConfigure = ()=>{
    const { update, currentRecord } = useContext(PermissionContext);
    const { t } = useTranslation();
    return /*#__PURE__*/ jsx(SchemaComponent, {
        components: {
            SnippetCheckboxGroup
        },
        schema: {
            type: 'void',
            name: 'form',
            'x-component': 'Form',
            'x-component-props': {
                useValues: (options)=>{
                    const api = useAPIClient();
                    return 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.data;
                            null == (_record_snippets = record.snippets) || _record_snippets.forEach((key)=>{
                                record[key] = true;
                            });
                            return {
                                data: record
                            };
                        }), options);
                },
                effects () {
                    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 };
