"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    AssociationFieldProvider: ()=>AssociationFieldProvider
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const isNewRecord_js_namespaceObject = require("../../../data-source/collection-record/isNewRecord.js");
const external_context_js_namespaceObject = require("./context.js");
const AssociationFieldProvider = (0, schema_namespaceObject.observer)((props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const field = (0, schema_namespaceObject.useField)();
    const { getCollectionJoinField, getCollection } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const allowMultiple = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.multiple) !== false;
    const allowDissociate = (null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1.allowDissociate) !== false;
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>getCollectionJoinField(fieldSchema['x-collection-field']), [
        fieldSchema['x-collection-field'],
        fieldSchema.name
    ]);
    const isFileCollection = (0, external_react_namespaceObject.useMemo)(()=>{
        var _getCollection;
        return (null === (_getCollection = getCollection(null == collectionField ? void 0 : collectionField.target)) || void 0 === _getCollection ? void 0 : _getCollection.template) === 'file';
    }, [
        fieldSchema['x-collection-field']
    ]);
    const isTreeCollection = (0, external_react_namespaceObject.useMemo)(()=>{
        var _getCollection;
        return (null === (_getCollection = getCollection(null == collectionField ? void 0 : collectionField.target)) || void 0 === _getCollection ? void 0 : _getCollection.template) === 'tree';
    }, [
        fieldSchema['x-collection-field']
    ]);
    const currentMode = (0, external_react_namespaceObject.useMemo)(()=>{
        var _fieldSchema_xcomponentprops;
        return (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) || (isFileCollection ? 'FileManager' : isTreeCollection ? 'Cascader' : 'Select');
    }, [
        null === (_fieldSchema_xcomponentprops2 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops2 ? void 0 : _fieldSchema_xcomponentprops2.mode
    ]);
    const fieldValue = (0, external_react_namespaceObject.useMemo)(()=>JSON.stringify(field.value), [
        field.value
    ]);
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(true);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setLoading(true);
        if (!collectionField) {
            setLoading(false);
            return;
        }
        if (field.value && field.form['__template'] && [
            'Nester',
            'SubTable',
            'PopoverNester'
        ].includes(currentMode)) {
            if ([
                'belongsTo',
                'hasOne'
            ].includes(collectionField.type)) {
                var _field_value;
                if (null === (_field_value = field.value) || void 0 === _field_value ? void 0 : _field_value[collectionField.targetKey]) delete field.value[collectionField.targetKey];
                field.value = {
                    ...field.initialValue,
                    ...field.value
                };
            } else if ([
                'belongsToMany',
                'hasMany'
            ].includes(collectionField.type)) {
                if (Array.isArray(field.value)) field.value = field.value.map((v)=>{
                    var _field_initialValue;
                    delete v[collectionField.targetKey];
                    return {
                        ...null === (_field_initialValue = field.initialValue) || void 0 === _field_initialValue ? void 0 : _field_initialValue[0],
                        ...v
                    };
                });
            }
        }
        if (null !== field.value && void 0 !== field.value) {
            if ([
                'Nester',
                'PopoverNester'
            ].includes(currentMode) && Array.isArray(field.value)) {
                if (0 === field.value.length && [
                    'belongsToMany',
                    'hasMany'
                ].includes(collectionField.type)) field.value = [
                    (0, isNewRecord_js_namespaceObject.markRecordAsNew)({})
                ];
            }
            setLoading(false);
            return;
        }
        if ([
            'Nester'
        ].includes(currentMode)) {
            if ([
                'belongsTo',
                'hasOne'
            ].includes(collectionField.type)) field.value = {};
            else if ([
                'belongsToMany',
                'hasMany'
            ].includes(collectionField.type)) field.value = [
                (0, isNewRecord_js_namespaceObject.markRecordAsNew)({})
            ];
        }
        if ('SubTable' === currentMode) field.value = [];
        setLoading(false);
    }, [
        currentMode,
        collectionField,
        fieldValue
    ]);
    if (loading) return null;
    return collectionField ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_js_namespaceObject.AssociationFieldContext.Provider, {
        value: {
            options: collectionField,
            field,
            allowMultiple,
            allowDissociate,
            currentMode
        },
        children: props.children
    }) : null;
}, {
    displayName: 'AssociationFieldProvider'
});
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
});
