"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    useValuesFromRecord: ()=>useValuesFromRecord,
    EditCollectionAction: ()=>EditCollectionAction,
    useUpdateCollectionActionAndRefreshCM: ()=>useUpdateCollectionActionAndRefreshCM,
    EditCollection: ()=>EditCollection
});
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 cloneDeep_namespaceObject = require("lodash/cloneDeep");
var cloneDeep_default = /*#__PURE__*/ __webpack_require__.n(cloneDeep_namespaceObject);
const omit_namespaceObject = require("lodash/omit");
var omit_default = /*#__PURE__*/ __webpack_require__.n(omit_namespaceObject);
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 getSchema = (schema, record, compile, getContainer)=>{
    if (!schema) return;
    const properties = cloneDeep_default()(schema.configurableProperties) || {
        title: {
            'x-component': 'CollectionField',
            'x-decorator': 'FormItem'
        },
        name: {
            'x-component': 'CollectionField',
            'x-decorator': 'FormItem'
        }
    };
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = cloneDeep_default()(schema.default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    return {
        type: 'object',
        properties: {
            [(0, schema_namespaceObject.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues: '{{ useValuesFromRecord }}'
                },
                title: '{{ t("Edit collection") }}',
                properties: {
                    ...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: '{{ useUpdateCollectionActionAndRefreshCM }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useValuesFromRecord = (options)=>{
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const result = (0, index_js_namespaceObject.useRequest)(()=>Promise.resolve({
            data: {
                ...omit_default()(cloneDeep_default()(record), [
                    '__parent',
                    '__collectionName'
                ]),
                category: null == record ? void 0 : record.category.map((v)=>v.id)
            }
        }), {
        ...options,
        manual: true
    });
    const ctx = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (ctx.visible) result.run();
    }, [
        ctx.visible
    ]);
    return result;
};
const useUpdateCollectionActionAndRefreshCM = (options)=>{
    const { refreshCM } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const form = (0, schema_namespaceObject.useForm)();
    const ctx = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const { refresh } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceActionContext)();
    const { resource, targetKey } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceContext)();
    const { [targetKey]: filterByTk, template } = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const collectionResource = 'sql' === template ? api.resource('sqlCollection') : resource;
    return {
        async run () {
            await form.submit();
            await collectionResource.update({
                filterByTk,
                values: 'sql' === template ? form.values : omit_default()(form.values, [
                    'fields'
                ])
            });
            ctx.setVisible(false);
            await form.reset();
            refresh();
            await refreshCM();
        }
    };
};
const EditCollection = (props)=>{
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EditCollectionAction, {
        item: record,
        ...props
    });
};
const EditCollectionAction = (props)=>{
    const { scope, getContainer, item: record, children, ...otherProps } = props;
    const { getTemplate } = (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 { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(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)("a", {
                    ...otherProps,
                    onClick: async ()=>{
                        const templateConf = getTemplate(record.template);
                        const schema = getSchema({
                            ...templateConf
                        }, record, compile, getContainer);
                        setSchema(schema);
                        setVisible(true);
                    },
                    children: children || t('Edit')
                }),
                /*#__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: {
                        getContainer,
                        useValuesFromRecord,
                        useUpdateCollectionActionAndRefreshCM,
                        useCancelAction: external_action_hooks_js_namespaceObject.useCancelAction,
                        createOnly: false,
                        ...scope
                    }
                })
            ]
        })
    });
};
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
});
