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_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__ from "../../data-source/collection-record/CollectionRecordProvider.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_useCollectionManager_deprecated_mjs__ from "../hooks/useCollectionManager_deprecated.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__ from "../ResourceActionProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__templates_components_sql_collection_index_mjs__ from "../templates/components/sql-collection/index.mjs";
const schema = {
    type: 'object',
    properties: {
        [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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': __WEBPACK_EXTERNAL_MODULE__templates_components_sql_collection_index_mjs__.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': __WEBPACK_EXTERNAL_MODULE__templates_components_sql_collection_index_mjs__.FieldsConfigure,
                            required: true
                        },
                        table: {
                            type: 'void',
                            title: '{{t("Preview")}}',
                            'x-decorator': 'FormItem',
                            'x-component': __WEBPACK_EXTERNAL_MODULE__templates_components_sql_collection_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useCollectionManager_deprecated_mjs__.useCollectionManager_deprecated)();
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceActionContext)();
    const { targetKey } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceContext)();
    const { [targetKey]: filterByTk } = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const parentRecordData = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionParentRecordData)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            initialValues: record
        }), [
        record
    ]);
    return 'sql' === record.template && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        parent: parentRecordData,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.FormProvider, {
            form: form,
            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__.Button, {
                        icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.SyncOutlined, {}),
                        onClick: (e)=>setVisible(true),
                        children: t('Sync from database')
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                        schema: schema,
                        components: {
                            FormLayout: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout
                        },
                        scope: {
                            useCancelAction: __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useCancelAction,
                            useSyncFromDB: ()=>useSyncFromDB(refreshCMList)
                        }
                    })
                ]
            })
        })
    });
};
export { SyncSQLFieldsAction };
