import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
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__association_field_util_mjs__ from "../../association-field/util.mjs";
const useSpecialCase = ()=>{
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const collectionField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getField(fieldSchema.name), [
        fieldSchema.name,
        getField
    ]);
    const isSpecialCase = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>isSpecialCaseField({
            collectionField,
            fieldSchema,
            getCollectionField
        }), [
        collectionField,
        fieldSchema,
        getCollectionField
    ]);
    const setDefaultValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__association_field_util_mjs__.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 = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].assignWith([
        ...oldValues
    ], newValues, (oldValue, newValue)=>{
        if (!oldValue && newValue) newValue.__notFromDatabase = true;
        return __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(field.value)) {
            const value = field.value;
            field.value = [
                (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.markRecordAsNew)({})
            ];
            setTimeout(()=>{
                field.value = value;
            });
        }
    }, []);
};
export { getParentFieldSchema, isFromDatabase, isSpecialCaseField, transformValue, useSpecialCase, useSubTableSpecialCase };
