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__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
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__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__ from "../action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__ from "../ResourceActionProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_TemplateSummary_mjs__ from "./components/TemplateSummary.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ImportCollectionMetaAction_mjs__ from "./ImportCollectionMetaAction.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ImportXlsx_ImportXlsxMetaAction_mjs__ from "./ImportXlsx/ImportXlsxMetaAction.mjs";
const getSchema = (schema, category, compile)=>{
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.configurableProperties);
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    const initialValue = {
        name: "t_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()),
        template: schema.name,
        view: 'view' === schema.name,
        category,
        ...(0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.default)
    };
    if (initialValue.reverseField) initialValue.reverseField.name = "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
    return {
        type: 'object',
        properties: {
            [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-component-props': {
                    getContainer: '{{ getContainer }}'
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
                                data: initialValue
                            }), options);
                    }
                },
                title: '{{ t("Create collection") }}',
                properties: {
                    summary: {
                        type: 'void',
                        'x-component': 'TemplateSummay',
                        'x-component-props': {
                            schemaKey: schema.name
                        }
                    },
                    ...properties,
                    footer: {
                        type: 'void',
                        'x-component': 'Action.Drawer.Footer',
                        properties: {
                            action1: {
                                title: '{{ t("Cancel") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    useAction: '{{ useCancelAction }}'
                                }
                            },
                            action2: {
                                title: '{{ t("Submit") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    type: 'primary',
                                    useAction: ()=>useCreateCollection(schema)
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useCreateCollection = (schema)=>{
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceActionContext)();
    const { resource } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    return {
        async run () {
            field.data = field.data || {};
            field.data.loading = true;
            try {
                var _schema_events;
                await form.submit();
                const values = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(form.values);
                if (null == schema ? void 0 : null === (_schema_events = schema.events) || void 0 === _schema_events ? void 0 : _schema_events.beforeSubmit) schema.events.beforeSubmit(values);
                if (!values.autoCreateReverseField) delete values.reverseField;
                delete values.autoCreateReverseField;
                await resource.create({
                    values: {
                        logging: true,
                        ...values
                    }
                });
                ctx.setVisible(false);
                await form.reset();
                field.data.loading = false;
                refresh();
                await refreshCM();
            } catch (error) {
                field.data.loading = false;
            }
        }
    };
};
const AddCollection = (props)=>{
    const recordData = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(AddCollectionAction, {
        item: recordData,
        ...props
    });
};
const AddCollectionAction = (props)=>{
    const { scope, getContainer, item: record, children, trigger, align } = props;
    const { getTemplate, templates: collectionTemplates } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schema, setSchema] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const importRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const importxlsxRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const items = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const result = [];
        collectionTemplates.forEach((item)=>{
            if (item.divider) result.push({
                type: 'divider'
            });
            result.push({
                label: compile(item.title),
                key: item.name
            });
        });
        return result;
    }, [
        collectionTemplates
    ]);
    const { state: { category } } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceActionContext)();
    const menu = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            style: {
                maxHeight: '60vh',
                overflow: 'auto'
            },
            onClick: (info)=>{
                if ('import' === info.key) {
                    var _importRef_current;
                    null === (_importRef_current = importRef.current) || void 0 === _importRef_current || _importRef_current.showModal();
                    return;
                }
                if ('importXlsx' === info.key) {
                    var _importxlsxRef_current;
                    null === (_importxlsxRef_current = importxlsxRef.current) || void 0 === _importxlsxRef_current || _importxlsxRef_current.showModal();
                    return;
                }
                const schema = getSchema(getTemplate(info.key), category, compile);
                setSchema(schema);
                setVisible(true);
            },
            items
        }), [
        category,
        items,
        importRef,
        importxlsxRef
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.ActionContextProvider, {
                value: {
                    visible,
                    setVisible
                },
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Dropdown, {
                        getPopupContainer: getContainer,
                        trigger: trigger,
                        align: align,
                        menu: menu,
                        children: children || /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                            icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                            type: 'primary',
                            children: [
                                t('Create collection'),
                                " ",
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DownOutlined, {})
                            ]
                        })
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                        schema: schema,
                        components: {
                            ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs__,
                            ArrayTable: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayTable,
                            TemplateSummay: __WEBPACK_EXTERNAL_MODULE__components_TemplateSummary_mjs__.TemplateSummary
                        },
                        scope: {
                            getContainer,
                            useCancelAction: __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useCancelAction,
                            createOnly: true,
                            useCreateCollection,
                            record,
                            showReverseFieldConfig: true,
                            ...scope
                        }
                    })
                ]
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ImportCollectionMetaAction_mjs__.ImportCollectionMetaAction, {
                ref: importRef
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ImportXlsx_ImportXlsxMetaAction_mjs__.ImportXlsxMetaAction, {
                ref: importxlsxRef
            })
        ]
    });
};
export { AddCollection, AddCollectionAction };
