import { useEffect, useMemo, useRef } from "react";
import { useField, useFieldSchema, useForm } from "@tachybase/schema";
import { nextTick } from "@tego/client";
import lodash from "lodash";
import { useAssociationNames } from "../../../../block-provider/hooks/index.mjs";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../../collection-manager/index.mjs";
import { useCollectionRecordData } from "../../../../data-source/collection-record/CollectionRecordProvider.mjs";
import { useFlag } from "../../../../flag-provider/index.mjs";
import { useVariables } from "../../../../variables/index.mjs";
import { transformVariableValue } from "../../../../variables/utils/transformVariableValue.mjs";
import { useSubFormValue } from "../../association-field/hooks.mjs";
import { isDisplayField } from "../utils.mjs";
const useLazyLoadDisplayAssociationFieldsOfForm_useLazyLoadDisplayAssociationFieldsOfForm = ()=>{
    var _sourceCollectionFieldRef_current;
    const { name } = useCollection_deprecated();
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    const form = useForm();
    const recordData = useCollectionRecordData();
    const fieldSchema = useFieldSchema();
    const variables = useVariables();
    const field = useField();
    const { formValue: subFormValue } = useSubFormValue();
    const { isInSubForm, isInSubTable } = useFlag() || {};
    const { getAssociationAppends } = useAssociationNames();
    const schemaName = fieldSchema.name.toString();
    const formValue = lodash.cloneDeep(isInSubForm || isInSubTable ? subFormValue : form.values);
    const collectionFieldRef = useRef(null);
    const sourceCollectionFieldRef = useRef(null);
    const hasPreloadData = useMemo(()=>hasPreload(recordData, schemaName), [
        recordData,
        schemaName
    ]);
    if (null == collectionFieldRef.current && isDisplayField(schemaName)) collectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName));
    if (null == sourceCollectionFieldRef.current && isDisplayField(schemaName)) sourceCollectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName.split('.')[0]));
    const sourceKeyValue = isDisplayField(schemaName) && sourceCollectionFieldRef.current ? lodash.get(formValue, "".concat(schemaName.split('.')[0], ".").concat((null == (_sourceCollectionFieldRef_current = sourceCollectionFieldRef.current) ? void 0 : _sourceCollectionFieldRef_current.targetKey) || 'id')) : void 0;
    useEffect(()=>{
        if (!isDisplayField(schemaName) || !variables || 'fields' === name || !collectionFieldRef.current || hasPreloadData) return;
        if (lodash.isEmpty(formValue)) return;
        const formVariable = {
            name: '$nForm',
            ctx: formValue,
            collectionName: name
        };
        const variableString = "{{ $nForm.".concat(schemaName, " }}");
        if (null == sourceKeyValue) return;
        const { appends } = getAssociationAppends();
        variables.parseVariable(variableString, formVariable, {
            appends
        }).then((value)=>{
            nextTick(()=>{
                const result = transformVariableValue(value, {
                    targetCollectionField: collectionFieldRef.current
                });
                if (lodash.isEmpty(result) && !lodash.isNumber(result)) field.value = null;
                else field.value = result;
            });
        }).catch((err)=>{
            console.error(err);
        });
    }, [
        sourceKeyValue
    ]);
};
const useLazyLoadDisplayAssociationFieldsOfForm = useLazyLoadDisplayAssociationFieldsOfForm_useLazyLoadDisplayAssociationFieldsOfForm;
function hasPreload(record, path) {
    const value = lodash.get(record, path);
    return null != value && '[{}]' !== JSON.stringify(value) && '{}' !== JSON.stringify(value);
}
export { useLazyLoadDisplayAssociationFieldsOfForm as default };
