"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    SystemSettingsPane: ()=>SystemSettingsPane
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("./index.js");
const external_index_js_namespaceObject_1 = require("../../index.js");
const index_js_namespaceObject = require("../../locale/index.js");
var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObject);
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const langs = Object.keys(index_js_default()).map((lang)=>({
        label: "".concat(index_js_default()[lang].label, " (").concat(lang, ")"),
        value: lang
    }));
const useCloseAction = ()=>{
    const { setVisible } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    return {
        async run () {
            setVisible(false);
        }
    };
};
const useSystemSettingsValues = (options)=>{
    const { visible } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const result = (0, external_index_js_namespaceObject.useSystemSettings)();
    return (0, external_index_js_namespaceObject_1.useRequest)(()=>Promise.resolve(result.data), {
        ...options,
        refreshDeps: [
            visible
        ]
    });
};
const useSaveSystemSettingsValues = ()=>{
    const { setVisible } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const form = (0, schema_namespaceObject.useForm)();
    const { message } = external_antd_namespaceObject.App.useApp();
    const { mutate, data } = (0, external_index_js_namespaceObject.useSystemSettings)();
    const api = (0, external_index_js_namespaceObject_1.useAPIClient)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    return {
        async run () {
            var _values_enabledLanguages;
            await form.submit();
            const values = (0, external_lodash_namespaceObject.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, schema_namespaceObject.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(external_index_js_namespaceObject_1.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Card, {
        bordered: false,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
            scope: {
                useSaveSystemSettingsValues,
                useSystemSettingsValues,
                useCloseAction
            },
            schema: schema
        })
    });
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
