"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__, {
    SyncSQLFieldsAction: ()=>SyncSQLFieldsAction
});
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_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../api-client/index.js");
const CollectionRecordProvider_js_namespaceObject = require("../../data-source/collection-record/CollectionRecordProvider.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 useCollectionManager_deprecated_js_namespaceObject = require("../hooks/useCollectionManager_deprecated.js");
const external_ResourceActionProvider_js_namespaceObject = require("../ResourceActionProvider.js");
const sql_collection_index_js_namespaceObject = require("../templates/components/sql-collection/index.js");
const schema = {
    type: 'object',
    properties: {
        [(0, schema_namespaceObject.uid)()]: {
            type: 'void',
            title: '{{ t("Sync from database") }}',
            'x-component': 'Action.Drawer',
            'x-decorator': 'FormLayout',
            'x-decorator-props': {
                layout: 'vertical'
            },
            properties: {
                config: {
                    type: 'void',
                    'x-decorator': sql_collection_index_js_namespaceObject.SQLRequestProvider,
                    'x-decorator-props': {
                        manual: false
                    },
                    properties: {
                        sql: {
                            type: 'string'
                        },
                        sources: {
                            type: 'array',
                            title: '{{t("Source collections")}}',
                            'x-decorator': 'FormItem',
                            'x-component': 'Select',
                            'x-component-props': {
                                multiple: true
                            },
                            'x-reactions': [
                                '{{useAsyncDataSource(loadCollections)}}'
                            ]
                        },
                        fields: {
                            type: 'array',
                            title: '{{t("Fields")}}',
                            'x-decorator': 'FormItem',
                            'x-component': sql_collection_index_js_namespaceObject.FieldsConfigure,
                            required: true
                        },
                        table: {
                            type: 'void',
                            title: '{{t("Preview")}}',
                            'x-decorator': 'FormItem',
                            'x-component': sql_collection_index_js_namespaceObject.PreviewTable
                        }
                    }
                },
                footer: {
                    type: 'void',
                    'x-component': 'Action.Drawer.Footer',
                    properties: {
                        cancel: {
                            title: '{{ t("Cancel") }}',
                            'x-component': 'Action',
                            'x-component-props': {
                                useAction: '{{ useCancelAction }}'
                            }
                        },
                        submit: {
                            title: '{{ t("Submit") }}',
                            'x-component': 'Action',
                            'x-component-props': {
                                type: 'primary',
                                useAction: '{{ useSyncFromDB }}'
                            }
                        }
                    }
                }
            }
        }
    }
};
const useSyncFromDB = (refreshCMList)=>{
    const form = (0, schema_namespaceObject.useForm)();
    const ctx = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const { refreshCM } = (0, useCollectionManager_deprecated_js_namespaceObject.useCollectionManager_deprecated)();
    const { refresh } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceActionContext)();
    const { targetKey } = (0, external_ResourceActionProvider_js_namespaceObject.useResourceContext)();
    const { [targetKey]: filterByTk } = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const field = (0, schema_namespaceObject.useField)();
    return {
        async run () {
            await form.submit();
            field.data = field.data || {};
            field.data.loading = true;
            try {
                await api.resource('sqlCollection').setFields({
                    filterByTk,
                    values: {
                        fields: form.values.fields,
                        sources: form.values.sources
                    }
                });
                ctx.setVisible(false);
                await form.reset();
                field.data.loading = false;
                refresh();
                await refreshCM();
                await (null == refreshCMList ? void 0 : refreshCMList());
            } catch (err) {
                field.data.loading = false;
            }
        }
    };
};
const SyncSQLFieldsAction = (param)=>{
    let { refreshCMList } = param;
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const parentRecordData = (0, CollectionRecordProvider_js_namespaceObject.useCollectionParentRecordData)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)({
            initialValues: record
        }), [
        record
    ]);
    return 'sql' === record.template && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
        record: record,
        parent: parentRecordData,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.FormProvider, {
            form: form,
            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.Button, {
                        icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.SyncOutlined, {}),
                        onClick: (e)=>setVisible(true),
                        children: t('Sync from database')
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
                        schema: schema,
                        components: {
                            FormLayout: components_namespaceObject.FormLayout
                        },
                        scope: {
                            useCancelAction: external_action_hooks_js_namespaceObject.useCancelAction,
                            useSyncFromDB: ()=>useSyncFromDB(refreshCMList)
                        }
                    })
                ]
            })
        })
    });
};
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
});
