"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__, {
    useSubTableSpecialCase: ()=>useSubTableSpecialCase,
    isSpecialCaseField: ()=>isSpecialCaseField,
    transformValue: ()=>transformValue,
    useSpecialCase: ()=>useSpecialCase,
    isFromDatabase: ()=>isFromDatabase,
    getParentFieldSchema: ()=>getParentFieldSchema
});
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 index_js_namespaceObject = require("../../../../collection-manager/index.js");
const isNewRecord_js_namespaceObject = require("../../../../data-source/collection-record/isNewRecord.js");
const util_js_namespaceObject = require("../../association-field/util.js");
const useSpecialCase = ()=>{
    const form = (0, schema_namespaceObject.useForm)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>getField(fieldSchema.name), [
        fieldSchema.name,
        getField
    ]);
    const isSpecialCase = (0, external_react_namespaceObject.useCallback)(()=>isSpecialCaseField({
            collectionField,
            fieldSchema,
            getCollectionField
        }), [
        collectionField,
        fieldSchema,
        getCollectionField
    ]);
    const setDefaultValue = (0, external_react_namespaceObject.useCallback)((value)=>{
        const parentFieldSchema = getParentFieldSchema(fieldSchema);
        if (parentFieldSchema) {
            const parentField = form.query(parentFieldSchema.name).take();
            if (parentField) parentField.setInitialValue(transformValue(value, {
                field: parentField,
                subFieldSchema: fieldSchema
            }));
        }
    }, [
        fieldSchema,
        form
    ]);
    return {
        isSpecialCase,
        setDefaultValue
    };
};
function getParentFieldSchema(fieldSchema) {
    while(null == fieldSchema ? void 0 : fieldSchema.parent){
        fieldSchema = fieldSchema.parent;
        if ((0, util_js_namespaceObject.isSubMode)(fieldSchema)) break;
    }
    return fieldSchema;
}
function isSpecialCaseField(param) {
    let { collectionField, fieldSchema, getCollectionField } = param;
    if (collectionField && [
        'hasOne',
        'belongsTo'
    ].includes(collectionField.type) && fieldSchema) {
        const parentFieldSchema = getParentFieldSchema(fieldSchema);
        if (parentFieldSchema && parentFieldSchema['x-collection-field']) {
            const parentCollectionField = getCollectionField(parentFieldSchema['x-collection-field']);
            if ([
                'hasMany',
                'belongsToMany'
            ].includes(null == parentCollectionField ? void 0 : parentCollectionField.type)) return true;
        }
    }
    return false;
}
function transformValue(value, deps) {
    const { field, subFieldSchema } = deps;
    const oldValues = field.value;
    let newValues = null;
    newValues = Array.isArray(value) ? value.map((item)=>({
            [subFieldSchema.name]: item
        })) : [
        {
            [subFieldSchema.name]: value
        }
    ];
    const result = external_lodash_default().assignWith([
        ...oldValues
    ], newValues, (oldValue, newValue)=>{
        if (!oldValue && newValue) newValue.__notFromDatabase = true;
        return external_lodash_default().assign({
            ...oldValues[0]
        }, newValue);
    });
    return result;
}
function isFromDatabase(value) {
    if (!value) return false;
    return !value.__notFromDatabase;
}
const useSubTableSpecialCase = (param)=>{
    let { field } = param;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (external_lodash_default().isEmpty(field.value)) {
            const value = field.value;
            field.value = [
                (0, isNewRecord_js_namespaceObject.markRecordAsNew)({})
            ];
            setTimeout(()=>{
                field.value = value;
            });
        }
    }, []);
};
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
});
