import { jsx } from "react/jsx-runtime";
import "react";
import { uid, useForm } from "@tachybase/schema";
import { Card, message } from "antd";
import { cloneDeep } from "lodash";
import { useTranslation } from "react-i18next";
import { useSystemSettings } from "./index.mjs";
import { i18n, useAPIClient, useRequest } from "../../index.mjs";
import locale from "../../locale/index.mjs";
import { SchemaComponent, useActionContext } from "../../schema-component/index.mjs";
const langs = Object.keys(locale).map((lang)=>({
        label: "".concat(locale[lang].label, " (").concat(lang, ")"),
        value: lang
    }));
const useCloseAction = ()=>{
    const { setVisible } = useActionContext();
    return {
        async run () {
            setVisible(false);
        }
    };
};
const useSystemSettingsValues = (options)=>{
    const { visible } = useActionContext();
    const result = useSystemSettings();
    return useRequest(()=>Promise.resolve(null == result ? void 0 : result.data), {
        ...options,
        refreshDeps: [
            visible,
            null == result ? void 0 : result.data
        ]
    });
};
const useSaveSystemSettingsValues = ()=>{
    const { setVisible } = useActionContext();
    const form = useForm();
    const { mutate, data } = useSystemSettings() || {};
    const api = useAPIClient();
    const { t } = useTranslation();
    return {
        async run () {
            var _values_enabledLanguages;
            await form.submit();
            const values = cloneDeep(form.values);
            mutate({
                data: {
                    ...null == data ? void 0 : data.data,
                    ...values
                }
            });
            await api.request({
                url: 'systemSettings:put',
                method: 'post',
                data: values
            });
            message.success(t('Saved successfully'));
            const lang = (null == (_values_enabledLanguages = 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: {
        [uid()]: {
            'x-decorator': 'Form',
            'x-decorator-props': {
                useValues: '{{ useSystemSettingsValues }}'
            },
            'x-component': 'div',
            type: 'void',
            title: '{{t("System settings")}}',
            properties: {
                raw_title: {
                    type: 'string',
                    title: "{{t('System title')}}",
                    'x-decorator': 'FormItem',
                    'x-component': 'TextAreaWithGlobalScope',
                    'x-component-props': {
                        supportsLineBreak: true
                    },
                    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[0]) === item.value) label += "(".concat(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__*/ jsx(Card, {
        bordered: false,
        children: /*#__PURE__*/ jsx(SchemaComponent, {
            scope: {
                useSaveSystemSettingsValues,
                useSystemSettingsValues,
                useCloseAction
            },
            schema: schema
        })
    });
export { SystemSettingsPane };
