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__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__ from "../../../data-source/collection-record/isNewRecord.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
const AssociationFieldProvider = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { getCollectionJoinField, getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getCollectionJoinField(fieldSchema['x-collection-field']), [
        fieldSchema['x-collection-field'],
        fieldSchema.name
    ]);
    const isFileCollection = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>JSON.stringify(field.value), [
        field.value
    ]);
    const [loading, setLoading] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.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, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.markRecordAsNew)({})
            ];
        }
        if ('SubTable' === currentMode) field.value = [];
        setLoading(false);
    }, [
        currentMode,
        collectionField,
        fieldValue
    ]);
    if (loading) return null;
    return collectionField ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.AssociationFieldContext.Provider, {
        value: {
            options: collectionField,
            field,
            allowMultiple,
            allowDissociate,
            currentMode
        },
        children: props.children
    }) : null;
}, {
    displayName: 'AssociationFieldProvider'
});
export { AssociationFieldProvider };
