"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    Editable: ()=>Editable
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../block-provider/hooks/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_AssociationFieldProvider_js_namespaceObject = require("./AssociationFieldProvider.js");
const external_AssociationSelect_js_namespaceObject = require("./AssociationSelect.js");
const CreateRecordAction_js_namespaceObject = require("./components/CreateRecordAction.js");
const external_FileManager_js_namespaceObject = require("./FileManager.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
const external_InternalCascader_js_namespaceObject = require("./InternalCascader.js");
const external_InternalCascadeSelect_js_namespaceObject = require("./InternalCascadeSelect.js");
const external_InternalDrawerSubTable_js_namespaceObject = require("./InternalDrawerSubTable.js");
const external_InternalNester_js_namespaceObject = require("./InternalNester.js");
const external_InternalPicker_js_namespaceObject = require("./InternalPicker.js");
const external_InternalPopoverNester_js_namespaceObject = require("./InternalPopoverNester.js");
const external_InternalSubTable_js_namespaceObject = require("./InternalSubTable.js");
const EditableAssociationField = (0, schema_namespaceObject.observer)((props)=>{
    const { multiple } = props;
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, schema_namespaceObject.useForm)();
    const { options: collectionField, currentMode } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const { components } = (0, external_react_namespaceObject.useContext)(schema_namespaceObject.SchemaOptionsContext);
    const AssociationSelect = external_lodash_default().get(components, 'AlternativeAssociationSelect') || external_AssociationSelect_js_namespaceObject.AssociationSelect;
    const useCreateActionProps = ()=>{
        const { onClick } = (0, index_js_namespaceObject.useAssociationCreateActionProps)();
        const actionField = (0, schema_namespaceObject.useField)();
        const { getPrimaryKey } = (0, external_collection_manager_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)(external_index_js_namespaceObject.SchemaComponentOptions, {
        scope: {
            useCreateActionProps
        },
        components: {
            CreateRecordAction: CreateRecordAction_js_namespaceObject.CreateRecordAction
        },
        children: [
            'Picker' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalPicker_js_namespaceObject.InternalPicker, {
                ...props
            }),
            'Nester' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalNester_js_namespaceObject.InternalNester, {
                ...props
            }),
            'PopoverNester' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalPopoverNester_js_namespaceObject.InternaPopoverNester, {
                ...props
            }),
            ('Select' === currentMode || 'CustomTitle' === currentMode) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AssociationSelect, {
                ...props
            }),
            'SubTable' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalSubTable_js_namespaceObject.InternalSubTable, {
                ...props
            }),
            'FileManager' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_FileManager_js_namespaceObject.InternalFileManager, {
                ...props
            }),
            'CascadeSelect' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalCascadeSelect_js_namespaceObject.InternalCascadeSelect, {
                ...props
            }),
            'DrawerSubTable' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalDrawerSubTable_js_namespaceObject.InternaDrawerSubTable, {
                ...props
            }),
            'Cascader' === currentMode && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_InternalCascader_js_namespaceObject.InternalCascader, {
                ...props
            })
        ]
    });
}, {
    displayName: 'EditableAssociationField'
});
const Editable = (0, schema_namespaceObject.observer)((props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_AssociationFieldProvider_js_namespaceObject.AssociationFieldProvider, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EditableAssociationField, {
            ...props
        })
    }), {
    displayName: 'Editable'
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
