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_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
const SettingCenterPermissionProvider = (props)=>{
    var _currentRecord_snippets;
    const { currentRecord } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(PermissionContext);
    if (!(null == currentRecord ? void 0 : null === (_currentRecord_snippets = currentRecord.snippets) || void 0 === _currentRecord_snippets ? void 0 : _currentRecord_snippets.includes('pm.*'))) return null;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: props.children
    });
};
const PermissionContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
PermissionContext.displayName = 'PermissionContext';
const PermissionProvider = (props)=>{
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { snippets } = record;
    null == snippets || snippets.forEach((key)=>{
        record[key] = true;
    });
    const [currentRecord, setCurrentRecord] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(record);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(PermissionContext.Provider, {
        value: {
            currentRecord,
            update: async (field, form)=>{
                const { path, value } = field.getState();
                if ([
                    'ui.*',
                    'pm',
                    'pm.*'
                ].includes(path)) {
                    const resource = api.resource('roles.snippets', record.name);
                    if (value) await resource.add({
                        values: [
                            path
                        ]
                    });
                    else await resource.remove({
                        values: [
                            path
                        ]
                    });
                    setCurrentRecord({
                        ...currentRecord,
                        ...form.values,
                        [path]: value
                    });
                } else {
                    await api.resource('roles').update({
                        filterByTk: record.name,
                        values: form.values
                    });
                    setCurrentRecord({
                        ...currentRecord,
                        ...form.values
                    });
                }
                message.success(t('Saved successfully'));
            }
        },
        children: props.children
    });
};
export { PermissionContext, PermissionProvider, SettingCenterPermissionProvider };
