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_file_saver__ from "file-saver";
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";
const TabsDesigner = ()=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const actionField = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.GeneralSchemaDesigner, {
        disableInitializer: true,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsModalItem, {
                title: t('Edit'),
                schema: {
                    type: 'object',
                    title: t('Edit tab'),
                    properties: {
                        title: {
                            title: t('Tab name'),
                            required: true,
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            'x-component-props': {}
                        },
                        icon: {
                            title: t('Icon'),
                            'x-decorator': 'FormItem',
                            'x-component': 'IconPicker',
                            'x-component-props': {}
                        }
                    }
                },
                initialValues: {
                    title: field.title,
                    icon: field.componentProps.icon
                },
                onSubmit: (param)=>{
                    let { title, icon } = param;
                    const props = fieldSchema['x-component-props'] || {};
                    fieldSchema.title = title;
                    field.title = title;
                    props.icon = icon;
                    field.componentProps.icon = icon;
                    fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                    fieldSchema['x-component-props'].icon = icon;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: fieldSchema['x-uid'],
                            title,
                            ['x-component-props']: props
                        }
                    });
                    dn.refresh();
                }
            }, "edit"),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsItem, {
                title: t('Dump'),
                onClick: async ()=>{
                    const deleteUid = (s)=>{
                        delete s['name'];
                        delete s['x-uid'];
                        Object.keys(s.properties || {}).forEach((key)=>{
                            deleteUid(s.properties[key]);
                        });
                    };
                    const { data } = await api.request({
                        url: "/uiSchemas:getJsonSchema/".concat(fieldSchema['x-uid'], "?includeAsyncNode=true")
                    });
                    const s = (null == data ? void 0 : data.data) || {};
                    deleteUid(s);
                    const blob = new Blob([
                        JSON.stringify(s, null, 2)
                    ], {
                        type: 'application/json'
                    });
                    (0, __WEBPACK_EXTERNAL_MODULE_file_saver__.saveAs)(blob, fieldSchema.parent['x-uid'] + '.schema.json');
                    message.success('Save successful!');
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsModalItem, {
                title: t('Load'),
                schema: {
                    'x-decorator': 'Form',
                    'x-component': 'Action.Modal',
                    'x-component-props': {
                        width: 520
                    },
                    type: 'void',
                    title: t('Load'),
                    properties: {
                        file: {
                            type: 'object',
                            title: '{{ t("File") }}',
                            required: true,
                            'x-decorator': 'FormItem',
                            'x-component': 'Upload.Attachment',
                            'x-component-props': {
                                action: 'attachments:create',
                                multiple: false
                            }
                        }
                    }
                },
                onSubmit: async (param)=>{
                    let { file } = param;
                    var _actionField;
                    var _data;
                    null !== (_data = (_actionField = actionField).data) && void 0 !== _data || (_actionField.data = {});
                    actionField.data.loading = true;
                    const { data } = await api.request({
                        url: file.url,
                        baseURL: '/'
                    });
                    const s = null != data ? data : {};
                    dn.insertAfterEnd(s);
                    actionField.data.loading = false;
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsRemove, {})
        ]
    });
};
export { TabsDesigner };
