"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    AddCollection: ()=>AddCollection,
    AddCollectionAction: ()=>AddCollectionAction
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_record_provider_index_js_namespaceObject = require("../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_action_hooks_js_namespaceObject = require("../action-hooks.js");
const external_hooks_index_js_namespaceObject = require("../hooks/index.js");
const external_ResourceActionProvider_js_namespaceObject = require("../ResourceActionProvider.js");
const external_components_index_js_namespaceObject = require("./components/index.js");
const TemplateSummary_js_namespaceObject = require("./components/TemplateSummary.js");
const external_ImportCollectionMetaAction_js_namespaceObject = require("./ImportCollectionMetaAction.js");
const getSchema = (schema, category, compile)=>{
    if (!schema) return;
    const properties = (0, external_lodash_namespaceObject.cloneDeep)(schema.configurableProperties);
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = (0, external_lodash_namespaceObject.cloneDeep)(schema.default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    const initialValue = {
        name: "t_".concat((0, schema_namespaceObject.uid)()),
        template: schema.name,
        view: 'view' === schema.name,
        category,
        ...(0, external_lodash_namespaceObject.cloneDeep)(schema.default)
    };
    if (initialValue.reverseField) initialValue.reverseField.name = "f_".concat((0, schema_namespaceObject.uid)());
    return {
        type: 'object',
        properties: {
            [(0, schema_namespaceObject.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-component-props': {
                    getContainer: '{{ getContainer }}'
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, index_js_namespaceObject.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, schema_namespaceObject.useForm)();
    const { refreshCM } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const ctx = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const { refresh } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceActionContext)();
    const { resource } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceContext)();
    const field = (0, schema_namespaceObject.useField)();
    return {
        async run () {
            field.data = field.data || {};
            field.data.loading = true;
            try {
                var _schema_events;
                await form.submit();
                const values = (0, external_lodash_namespaceObject.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, external_record_provider_index_js_namespaceObject.useRecord)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AddCollectionAction, {
        item: recordData,
        ...props
    });
};
const AddCollectionAction = (props)=>{
    const { scope, getContainer, item: record, children, trigger, align } = props;
    const { getTemplate, templates: collectionTemplates } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const [schema, setSchema] = (0, external_react_namespaceObject.useState)({});
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const importRef = (0, external_react_namespaceObject.useRef)(null);
    const items = (0, external_react_namespaceObject.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, external_ResourceActionProvider_js_namespaceObject.useResourceActionContext)();
    const menu = (0, external_react_namespaceObject.useMemo)(()=>({
            style: {
                maxHeight: '60vh',
                overflow: 'auto'
            },
            onClick: (info)=>{
                if ('import' === info.key) {
                    var _importRef_current;
                    console.log('import', importRef.current);
                    null === (_importRef_current = importRef.current) || void 0 === _importRef_current || _importRef_current.showModal();
                    return;
                }
                const schema = getSchema(getTemplate(info.key), category, compile);
                setSchema(schema);
                setVisible(true);
            },
            items
        }), [
        category,
        items,
        importRef
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_record_provider_index_js_namespaceObject.RecordProvider, {
        record: record,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_component_index_js_namespaceObject.ActionContextProvider, {
                value: {
                    visible,
                    setVisible
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Dropdown, {
                        getPopupContainer: getContainer,
                        trigger: trigger,
                        align: align,
                        menu: menu,
                        children: children || /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Button, {
                            icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                            type: 'primary',
                            children: [
                                t('Create collection'),
                                " ",
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DownOutlined, {})
                            ]
                        })
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
                        schema: schema,
                        components: {
                            ...external_components_index_js_namespaceObject,
                            ArrayTable: components_namespaceObject.ArrayTable,
                            TemplateSummay: TemplateSummary_js_namespaceObject.TemplateSummary
                        },
                        scope: {
                            getContainer,
                            useCancelAction: external_action_hooks_js_namespaceObject.useCancelAction,
                            createOnly: true,
                            useCreateCollection,
                            record,
                            showReverseFieldConfig: true,
                            ...scope
                        }
                    })
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ImportCollectionMetaAction_js_namespaceObject.ImportCollectionMetaAction, {
                ref: importRef
            })
        ]
    });
};
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
});
