import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "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__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "./index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__locale_index_mjs__ from "../../locale/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
const langs = Object.keys(__WEBPACK_EXTERNAL_MODULE__locale_index_mjs__["default"]).map((lang)=>({
        label: "".concat(__WEBPACK_EXTERNAL_MODULE__locale_index_mjs__["default"][lang].label, " (").concat(lang, ")"),
        value: lang
    }));
const useCloseAction = ()=>{
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    return {
        async run () {
            setVisible(false);
        }
    };
};
const useSystemSettingsValues = (options)=>{
    const { visible } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const result = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSystemSettings)();
    return (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(()=>Promise.resolve(result.data), {
        ...options,
        refreshDeps: [
            visible
        ]
    });
};
const useSaveSystemSettingsValues = ()=>{
    const { setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { mutate, data } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSystemSettings)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return {
        async run () {
            var _values_enabledLanguages;
            await form.submit();
            const values = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(form.values);
            mutate({
                data: {
                    ...null == data ? void 0 : data.data,
                    ...values
                }
            });
            await api.request({
                url: 'systemSettings:update/1',
                method: 'post',
                data: values
            });
            message.success(t('Saved successfully'));
            const lang = (null === (_values_enabledLanguages = values.enabledLanguages) || void 0 === _values_enabledLanguages ? void 0 : _values_enabledLanguages[0]) || 'en-US';
            if (values.enabledLanguages.length < 2 && api.auth.getLocale() !== lang) {
                api.auth.setLocale('');
                window.location.reload();
            } else setVisible(false);
        }
    };
};
const schema = {
    type: 'object',
    properties: {
        [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
            'x-decorator': 'Form',
            'x-decorator-props': {
                useValues: '{{ useSystemSettingsValues }}'
            },
            'x-component': 'div',
            type: 'void',
            title: '{{t("System settings")}}',
            properties: {
                title: {
                    type: 'string',
                    title: "{{t('System title')}}",
                    'x-decorator': 'FormItem',
                    'x-component': 'Input',
                    required: true
                },
                logo: {
                    type: 'string',
                    title: "{{t('Logo')}}",
                    'x-decorator': 'FormItem',
                    'x-component': 'Upload.Attachment',
                    'x-component-props': {
                        action: 'attachments:create',
                        multiple: false
                    }
                },
                enabledLanguages: {
                    type: 'array',
                    title: '{{t("Enabled languages")}}',
                    'x-component': 'Select',
                    'x-component-props': {
                        mode: 'multiple'
                    },
                    'x-decorator': 'FormItem',
                    enum: langs,
                    'x-reactions': (field)=>{
                        field.dataSource = langs.map((item)=>{
                            var _field_value;
                            let label = item.label;
                            if ((null === (_field_value = field.value) || void 0 === _field_value ? void 0 : _field_value[0]) === item.value) label += "(".concat(__WEBPACK_EXTERNAL_MODULE__index_mjs__.i18n.t('Default'), ")");
                            return {
                                label,
                                value: item.value
                            };
                        });
                    }
                },
                footer1: {
                    type: 'void',
                    'x-component': 'ActionBar',
                    'x-component-props': {
                        layout: 'one-column'
                    },
                    properties: {
                        submit: {
                            title: '{{t("Submit")}}',
                            'x-component': 'Action',
                            'x-component-props': {
                                type: 'primary',
                                htmlType: 'submit',
                                useAction: '{{ useSaveSystemSettingsValues }}'
                            }
                        }
                    }
                }
            }
        }
    }
};
const SystemSettingsPane = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
        bordered: false,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
            scope: {
                useSaveSystemSettingsValues,
                useSystemSettingsValues,
                useCloseAction
            },
            schema: schema
        })
    });
export { SystemSettingsPane };
