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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__ from "../../../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AssociationFieldProvider_mjs__ from "./AssociationFieldProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AssociationSelect_mjs__ from "./AssociationSelect.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_CreateRecordAction_mjs__ from "./components/CreateRecordAction.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FileManager_mjs__ from "./FileManager.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalCascader_mjs__ from "./InternalCascader.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalCascadeSelect_mjs__ from "./InternalCascadeSelect.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalDrawerSubTable_mjs__ from "./InternalDrawerSubTable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalNester_mjs__ from "./InternalNester.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalPicker_mjs__ from "./InternalPicker.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalPopoverNester_mjs__ from "./InternalPopoverNester.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalSubTable_mjs__ from "./InternalSubTable.mjs";
const EditableAssociationField = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { multiple } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { options: collectionField, currentMode } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useAssociationFieldContext)();
    const { components } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaOptionsContext);
    const AssociationSelect = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].get(components, 'AlternativeAssociationSelect') || __WEBPACK_EXTERNAL_MODULE__AssociationSelect_mjs__.AssociationSelect;
    const useCreateActionProps = ()=>{
        const { onClick } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useAssociationCreateActionProps)();
        const actionField = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
        const { getPrimaryKey } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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 === _actionField_data ? void 0 : null === (_actionField_data_data = _actionField_data.data) || void 0 === _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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponentOptions, {
        scope: {
            useCreateActionProps
        },
        components: {
            CreateRecordAction: __WEBPACK_EXTERNAL_MODULE__components_CreateRecordAction_mjs__.CreateRecordAction
        },
        children: [
            'Picker' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalPicker_mjs__.InternalPicker, {
                ...props
            }),
            'Nester' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalNester_mjs__.InternalNester, {
                ...props
            }),
            'PopoverNester' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalPopoverNester_mjs__.InternaPopoverNester, {
                ...props
            }),
            'Select' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(AssociationSelect, {
                ...props
            }),
            'SubTable' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalSubTable_mjs__.InternalSubTable, {
                ...props
            }),
            'FileManager' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__FileManager_mjs__.InternalFileManager, {
                ...props
            }),
            'CascadeSelect' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalCascadeSelect_mjs__.InternalCascadeSelect, {
                ...props
            }),
            'DrawerSubTable' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalDrawerSubTable_mjs__.InternaDrawerSubTable, {
                ...props
            }),
            'Cascader' === currentMode && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__InternalCascader_mjs__.InternalCascader, {
                ...props
            })
        ]
    });
}, {
    displayName: 'EditableAssociationField'
});
const Editable = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__AssociationFieldProvider_mjs__.AssociationFieldProvider, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(EditableAssociationField, {
            ...props
        })
    }), {
    displayName: 'Editable'
});
export { Editable };
