import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { uid } from "@tachybase/schema";
import { ArrayTable } from "@tego/client";
import { cloneDeep, omit, set } from "lodash";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../../api-client/index.mjs";
import { RecordProvider } from "../../../record-provider/index.mjs";
import { ActionContextProvider, SchemaComponent, useCompile } from "../../../schema-component/index.mjs";
import { useCancelAction } from "../../action-hooks.mjs";
import { useCollectionManager_deprecated } from "../../hooks/index.mjs";
import useDialect from "../../hooks/useDialect.mjs";
import { setUpdateXlsxCollectionField } from "./setUpdateXlsxCollectionField.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs_66df194d__ from "../components/index.mjs";
const XlsxEditFieldAction = (props)=>{
    const { scope, getContainer, record, parentItem: parentRecord, children, handleUiSchemaChange, ...otherProps } = props;
    const { getInterface, collections, getCollection } = useCollectionManager_deprecated();
    const [visible, setVisible] = useState(false);
    const [schema, setSchema] = useState({});
    useAPIClient();
    const { t } = useTranslation();
    const compile = useCompile();
    const [data, setData] = useState({
        record
    });
    const { isDialect } = useDialect();
    return /*#__PURE__*/ jsx(RecordProvider, {
        record: record,
        parent: parentRecord,
        children: /*#__PURE__*/ jsxs(ActionContextProvider, {
            value: {
                visible,
                setVisible
            },
            children: [
                /*#__PURE__*/ jsx("a", {
                    ...otherProps,
                    onClick: async ()=>{
                        setData(record);
                        const interfaceConf = getInterface(record.interface);
                        const defaultValues = 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.reverseField;
                            set(defaultValues.reverseField, 'name', record.name);
                            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__*/ jsx(SchemaComponent, {
                    schema: schema,
                    components: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs_66df194d__,
                        ArrayTable: ArrayTable
                    },
                    scope: {
                        getContainer,
                        useCancelAction: useCancelAction,
                        showReverseFieldConfig: !(null == data ? void 0 : data.reverseField),
                        isDialect,
                        setUpdateXlsxCollectionField: setUpdateXlsxCollectionField,
                        disabledJSONB: true,
                        createMainOnly: true,
                        ...scope
                    }
                })
            ]
        })
    });
};
const getSchema = (schema, record, compile, getContainer)=>{
    if (!schema) return;
    const properties = cloneDeep(schema.properties);
    if (null == properties ? void 0 : properties.name) properties.name['x-disabled'] = true;
    if (true === schema.hasDefaultValue) {
        properties['defaultValue'] = 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: {
            [uid()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-component-props': {
                    getContainer: getContainer
                },
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return useRequest(()=>Promise.resolve({
                                data: cloneDeep(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 };
