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_lodash__ from "lodash";
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__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_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__ from "../ResourceActionProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__templates_components_PreviewFields_mjs__ from "../templates/components/PreviewFields.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__templates_components_PreviewTable_mjs__ from "../templates/components/PreviewTable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
const getSchema = (schema, record, compile)=>{
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.properties);
    if (true === schema.hasDefaultValue) {
        var _schema_default;
        properties['defaultValue'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(null == schema ? void 0 : null === (_schema_default = schema.default) || void 0 === _schema_default ? void 0 : _schema_default.uiSchema);
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
    }
    const initialValue = {
        name: "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()),
        ...(0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.default),
        interface: schema.name
    };
    if (initialValue.reverseField) initialValue.reverseField.name = "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
    return {
        type: 'object',
        properties: {
            [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-component-props': {
                    getContainer: '{{ getContainer }}'
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
                                data: initialValue
                            }), options);
                    }
                },
                title: "".concat(compile('{{ t("Sync from database") }}')),
                properties: {
                    schema: {
                        type: 'string',
                        'x-hidden': true,
                        default: null == record ? void 0 : record.schema
                    },
                    viewName: {
                        type: 'string',
                        'x-hidden': true,
                        default: null == record ? void 0 : record.viewName
                    },
                    fields: {
                        type: 'array',
                        'x-component': __WEBPACK_EXTERNAL_MODULE__templates_components_PreviewFields_mjs__.PreviewFields,
                        'x-component-props': {
                            ...record
                        },
                        default: record.fields
                    },
                    preview: {
                        type: 'object',
                        'x-component': __WEBPACK_EXTERNAL_MODULE__templates_components_PreviewTable_mjs__.PreviewTable,
                        'x-component-props': {
                            ...record
                        },
                        'x-reactions': {
                            dependencies: [
                                'fields'
                            ],
                            fulfill: {
                                schema: {
                                    'x-component-props': '{{{...record,...$form.values}}}'
                                }
                            }
                        }
                    },
                    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: '{{ useSyncFromDatabase }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useSyncFromDatabase = ()=>{
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    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("collections").setFields({
                    filterByTk,
                    values: form.values
                });
                ctx.setVisible(false);
                await form.reset();
                field.data.loading = false;
                refresh();
                await refreshCM();
            } catch (error) {
                field.data.loading = false;
            }
        }
    };
};
const SyncFieldsAction = (props)=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SyncFieldsActionCom, {
        item: record,
        ...props
    });
};
const SyncFieldsActionCom = (props)=>{
    const { scope, getContainer, item: record, children } = props;
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schema, setSchema] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return 'view' === record.template && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.ActionContextProvider, {
            value: {
                visible,
                setVisible,
                drawerProps: {
                    width: 900
                }
            },
            children: [
                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__.PlusOutlined, {}),
                    onClick: (e)=>{
                        const schema = getSchema({}, record, compile);
                        if (schema) {
                            setSchema(schema);
                            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: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs__,
                        ArrayTable: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayTable
                    },
                    scope: {
                        getContainer,
                        useCancelAction: __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useCancelAction,
                        createOnly: true,
                        isOverride: false,
                        useSyncFromDatabase,
                        record,
                        ...scope
                    }
                })
            ]
        })
    });
};
export { SyncFieldsAction, SyncFieldsActionCom };
