"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__, {
    AddSubFieldAction: ()=>AddSubFieldAction
});
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_data_source_index_js_namespaceObject = require("../../data-source/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_hooks_index_js_namespaceObject = require("../hooks/index.js");
const external_components_index_js_namespaceObject = require("./components/index.js");
const external_interfaces_js_namespaceObject = require("./interfaces.js");
const getSchema = (schema)=>{
    if (!schema) return;
    const properties = (0, external_lodash_namespaceObject.cloneDeep)(schema.properties);
    const initialValue = {
        name: "f_".concat((0, schema_namespaceObject.uid)()),
        ...(0, external_lodash_namespaceObject.cloneDeep)(schema.default),
        interface: schema.name
    };
    return {
        type: 'object',
        properties: {
            [(0, schema_namespaceObject.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, index_js_namespaceObject.useRequest)(()=>Promise.resolve({
                                data: initialValue
                            }), options);
                    }
                },
                title: '{{ t("Add field") }}',
                properties: {
                    ...properties,
                    footer: {
                        type: 'void',
                        'x-component': 'Action.Drawer.Footer',
                        properties: {
                            action1: {
                                title: '{{ t("Cancel") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    useAction: '{{ cm.useCancelAction }}'
                                }
                            },
                            action2: {
                                title: '{{ t("Submit") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    type: 'primary',
                                    useAction: '{{ ds.useCreateAction }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useCreateSubField = ()=>{
    (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    return {
        async run () {}
    };
};
const AddSubFieldAction = ()=>{
    const { getInterface } = (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 options = (0, external_interfaces_js_namespaceObject.useFieldInterfaceOptions)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const items = (0, external_react_namespaceObject.useMemo)(()=>options.map((option)=>{
            const children = option.children.map((child)=>({
                    label: compile(child.title),
                    key: child.name
                }));
            return {
                label: compile(option.label),
                key: option.key,
                children
            };
        }), [
        options
    ]);
    const menu = (0, external_react_namespaceObject.useMemo)(()=>({
            style: {
                maxHeight: '60vh',
                overflow: 'auto'
            },
            onClick: (info)=>{
                const schema = getSchema(getInterface(info.key));
                setSchema(schema);
                setVisible(true);
            },
            items
        }), [
        items
    ]);
    const recordData = (0, external_data_source_index_js_namespaceObject.useCollectionRecordData)();
    return /*#__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, {
                menu: menu,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                    icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                    type: 'primary',
                    children: t('Add field')
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
                record: {},
                parent: recordData,
                children: /*#__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
                    },
                    scope: {
                        createOnly: true,
                        useCreateSubField
                    }
                })
            })
        ]
    });
};
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
});
