import { useCallback, useEffect, useMemo } from "react";
import { useFieldSchema, useForm } from "@tachybase/schema";
import lodash from "lodash";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { markRecordAsNew } from "../../../../data-source/collection-record/isNewRecord.mjs";
import { isSubMode } from "../../association-field/util.mjs";
const useSpecialCase = ()=>{
    const form = useForm();
    const fieldSchema = useFieldSchema();
    const { getField } = useCollection_deprecated();
    const { getCollectionField } = useCollectionManager_deprecated();
    const collectionField = useMemo(()=>getField(fieldSchema.name), [
        fieldSchema.name,
        getField
    ]);
    const isSpecialCase = useCallback(()=>isSpecialCaseField({
            collectionField,
            fieldSchema,
            getCollectionField
        }), [
        collectionField,
        fieldSchema,
        getCollectionField
    ]);
    const setDefaultValue = 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 (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 = lodash.assignWith([
        ...oldValues
    ], newValues, (oldValue, newValue)=>{
        if (!oldValue && newValue) newValue.__notFromDatabase = true;
        return lodash.assign({
            ...oldValues[0]
        }, newValue);
    });
    return result;
}
function isFromDatabase(value) {
    if (!value) return false;
    return !value.__notFromDatabase;
}
const useSubTableSpecialCase = (param)=>{
    let { field } = param;
    useEffect(()=>{
        if (lodash.isEmpty(field.value)) {
            const value = field.value;
            field.value = [
                markRecordAsNew({})
            ];
            setTimeout(()=>{
                field.value = value;
            });
        }
    }, []);
};
export { getParentFieldSchema, isFromDatabase, isSpecialCaseField, transformValue, useSpecialCase, useSubTableSpecialCase };
