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_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__hooks_useDialect_mjs__ from "../../hooks/useDialect.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "../components/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__setUpdateXlsxCollectionField_mjs__ from "./setUpdateXlsxCollectionField.mjs";
const XlsxEditFieldAction = (props)=>{
    const { scope, getContainer, record, parentItem: parentRecord, children, handleUiSchemaChange, ...otherProps } = props;
    const { getInterface, collections, getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schema, setSchema] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const [data, setData] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({
        record
    });
    const { isDialect } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useDialect_mjs__["default"])();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: record,
        parent: parentRecord,
        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)("a", {
                    ...otherProps,
                    onClick: async ()=>{
                        setData(record);
                        const interfaceConf = getInterface(record.interface);
                        const defaultValues = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(record) || {};
                        if (!(null == defaultValues ? void 0 : defaultValues.reverseField)) {
                            var _interfaceConf_default;
                            defaultValues.autoCreateReverseField = false;
                            defaultValues.reverseField = null == interfaceConf ? void 0 : null === (_interfaceConf_default = interfaceConf.default) || void 0 === _interfaceConf_default ? void 0 : _interfaceConf_default.reverseField;
                            (0, __WEBPACK_EXTERNAL_MODULE_lodash__.set)(defaultValues.reverseField, 'name', record.name);
                            (0, __WEBPACK_EXTERNAL_MODULE_lodash__.set)(defaultValues, 'uiSchema.title', record.uiSchema.title);
                        }
                        const schema = getSchema({
                            ...interfaceConf,
                            default: defaultValues
                        }, record, compile, getContainer);
                        setSchema(schema);
                        setVisible(true);
                    },
                    children: children || t('Edit')
                }),
                /*#__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,
                        showReverseFieldConfig: !(null == data ? void 0 : data.reverseField),
                        isDialect,
                        setUpdateXlsxCollectionField: __WEBPACK_EXTERNAL_MODULE__setUpdateXlsxCollectionField_mjs__.setUpdateXlsxCollectionField,
                        disabledJSONB: true,
                        createMainOnly: true,
                        ...scope
                    }
                })
            ]
        })
    });
};
const getSchema = (schema, record, compile, getContainer)=>{
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.properties);
    if (null == properties ? void 0 : properties.name) properties.name['x-disabled'] = true;
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(schema.default) || {};
        properties.defaultValue.required = false;
        properties['defaultValue']['title'] = compile('{{ t("Default value") }}');
        properties['defaultValue']['x-decorator'] = 'FormItem';
        properties['defaultValue']['x-reactions'] = [
            {
                dependencies: [
                    'uiSchema.x-component-props.gmt',
                    'uiSchema.x-component-props.showTime',
                    'uiSchema.x-component-props.dateFormat',
                    'uiSchema.x-component-props.timeFormat'
                ],
                fulfill: {
                    state: {
                        componentProps: {
                            gmt: '{{$deps[0]}}',
                            showTime: '{{$deps[1]}}',
                            dateFormat: '{{$deps[2]}}',
                            timeFormat: '{{$deps[3]}}'
                        }
                    }
                }
            },
            {
                dependencies: [
                    'primaryKey',
                    'unique',
                    'autoIncrement'
                ],
                when: '{{$deps[0]||$deps[1]||$deps[2]}}',
                fulfill: {
                    state: {
                        hidden: true,
                        value: void 0
                    }
                },
                otherwise: {
                    state: {
                        hidden: false
                    }
                }
            }
        ];
    }
    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: (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)((0, __WEBPACK_EXTERNAL_MODULE_lodash__.omit)(schema.default, [
                                    'uiSchema.rawTitle'
                                ]))
                            }), options);
                    }
                },
                title: "".concat(compile(record.uiSchema.title), " - ").concat(compile('{{ t("Edit field") }}')),
                properties: {
                    summary: {
                        type: 'void',
                        'x-component': 'FieldSummary',
                        'x-component-props': {
                            schemaKey: schema.name
                        }
                    },
                    ...properties,
                    description: {
                        type: 'string',
                        title: '{{t("Description")}}',
                        'x-decorator': 'FormItem',
                        'x-component': 'Input.TextArea'
                    },
                    footer: {
                        type: 'void',
                        'x-component': 'Action.Drawer.Footer',
                        properties: {
                            action1: {
                                title: '{{ t("Cancel") }}',
                                'x-component': 'Action',
                                'x-use-component-props': 'useCancelActionProps'
                            },
                            action2: {
                                title: '{{ t("Submit") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    type: 'primary'
                                },
                                'x-use-component-props': 'setUpdateXlsxCollectionField'
                            }
                        }
                    }
                }
            }
        }
    };
};
export { XlsxEditFieldAction };
