import { jsx, jsxs } from "react/jsx-runtime";
import { useContext } from "react";
import { SchemaOptionsContext, observer, useField, useForm } from "@tachybase/schema";
import lodash from "lodash";
import { SchemaComponentOptions } from "../../index.mjs";
import { useAssociationCreateActionProps } from "../../../block-provider/hooks/index.mjs";
import { useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { AssociationFieldProvider } from "./AssociationFieldProvider.mjs";
import { AssociationSelect as external_AssociationSelect_mjs_AssociationSelect } from "./AssociationSelect.mjs";
import { CreateRecordAction } from "./components/CreateRecordAction.mjs";
import { InternalFileManager } from "./FileManager.mjs";
import { useAssociationFieldContext } from "./hooks.mjs";
import { InternalCascader } from "./InternalCascader.mjs";
import { InternalCascadeSelect } from "./InternalCascadeSelect.mjs";
import { InternaDrawerSubTable } from "./InternalDrawerSubTable.mjs";
import { InternalNester } from "./InternalNester.mjs";
import { InternalPicker } from "./InternalPicker.mjs";
import { InternaPopoverNester } from "./InternalPopoverNester.mjs";
import { InternalSubTable } from "./InternalSubTable.mjs";
const EditableAssociationField = observer((props)=>{
    const { multiple } = props;
    const field = useField();
    const form = useForm();
    const { options: collectionField, currentMode } = useAssociationFieldContext();
    const { components } = useContext(SchemaOptionsContext);
    const AssociationSelect = lodash.get(components, 'AlternativeAssociationSelect') || external_AssociationSelect_mjs_AssociationSelect;
    const useCreateActionProps = ()=>{
        const { onClick } = useAssociationCreateActionProps();
        const actionField = useField();
        const { getPrimaryKey } = useCollection_deprecated();
        const primaryKey = getPrimaryKey();
        return {
            async onClick () {
                var _actionField_data_data, _actionField_data;
                await onClick();
                const { data } = (null == (_actionField_data = actionField.data) ? void 0 : null == (_actionField_data_data = _actionField_data.data) ? void 0 : _actionField_data_data.data) || {};
                if (data) if ([
                    'm2m',
                    'o2m'
                ].includes(null == collectionField ? void 0 : collectionField.interface) && false !== multiple) {
                    const values = form.getValuesIn(field.path) || [];
                    if (!values.find((v)=>v[primaryKey] === data[primaryKey])) {
                        values.push(data);
                        form.setValuesIn(field.path, values);
                        field.onInput(values);
                    }
                } else {
                    form.setValuesIn(field.path, data);
                    field.onInput(data);
                }
            }
        };
    };
    return /*#__PURE__*/ jsxs(SchemaComponentOptions, {
        scope: {
            useCreateActionProps
        },
        components: {
            CreateRecordAction: CreateRecordAction
        },
        children: [
            'Picker' === currentMode && /*#__PURE__*/ jsx(InternalPicker, {
                ...props
            }),
            'Nester' === currentMode && /*#__PURE__*/ jsx(InternalNester, {
                ...props
            }),
            'PopoverNester' === currentMode && /*#__PURE__*/ jsx(InternaPopoverNester, {
                ...props
            }),
            ('Select' === currentMode || 'CustomTitle' === currentMode) && /*#__PURE__*/ jsx(AssociationSelect, {
                ...props
            }),
            'SubTable' === currentMode && /*#__PURE__*/ jsx(InternalSubTable, {
                ...props
            }),
            'FileManager' === currentMode && /*#__PURE__*/ jsx(InternalFileManager, {
                ...props
            }),
            'CascadeSelect' === currentMode && /*#__PURE__*/ jsx(InternalCascadeSelect, {
                ...props
            }),
            'DrawerSubTable' === currentMode && /*#__PURE__*/ jsx(InternaDrawerSubTable, {
                ...props
            }),
            'Cascader' === currentMode && /*#__PURE__*/ jsx(InternalCascader, {
                ...props
            })
        ]
    });
}, {
    displayName: 'EditableAssociationField'
});
const Editable = observer((props)=>/*#__PURE__*/ jsx(AssociationFieldProvider, {
        children: /*#__PURE__*/ jsx(EditableAssociationField, {
            ...props
        })
    }), {
    displayName: 'Editable'
});
export { Editable };
