import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__ from "../../../../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__ from "../../../../data-source/collection-record/CollectionRecordProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__ from "../../../../flag-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../../../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_transformVariableValue_mjs__ from "../../../../variables/utils/transformVariableValue.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__association_field_hooks_mjs__ from "../../association-field/hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
const useLazyLoadDisplayAssociationFieldsOfForm = ()=>{
    var _sourceCollectionFieldRef_current;
    const { name } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const recordData = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionRecordData)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { formValue: subFormValue } = (0, __WEBPACK_EXTERNAL_MODULE__association_field_hooks_mjs__.useSubFormValue)();
    const { isInSubForm, isInSubTable } = (0, __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.useFlag)() || {};
    const { getAssociationAppends } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useAssociationNames)();
    const schemaName = fieldSchema.name.toString();
    const formValue = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(isInSubForm || isInSubTable ? subFormValue : form.values);
    const collectionFieldRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const sourceCollectionFieldRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const hasPreloadData = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>hasPreload(recordData, schemaName), [
        recordData,
        schemaName
    ]);
    if (null == collectionFieldRef.current && (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.isDisplayField)(schemaName)) collectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName));
    if (null == sourceCollectionFieldRef.current && (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.isDisplayField)(schemaName)) sourceCollectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName.split('.')[0]));
    const sourceKeyValue = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.isDisplayField)(schemaName) && sourceCollectionFieldRef.current ? __WEBPACK_EXTERNAL_MODULE_lodash__["default"].get(formValue, "".concat(schemaName.split('.')[0], ".").concat((null === (_sourceCollectionFieldRef_current = sourceCollectionFieldRef.current) || void 0 === _sourceCollectionFieldRef_current ? void 0 : _sourceCollectionFieldRef_current.targetKey) || 'id')) : void 0;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!(0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.isDisplayField)(schemaName) || !variables || 'fields' === name || !collectionFieldRef.current || hasPreloadData) return;
        if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].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)=>{
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.nextTick)(()=>{
                const result = (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_transformVariableValue_mjs__.transformVariableValue)(value, {
                    targetCollectionField: collectionFieldRef.current
                });
                if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(result) && !__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isNumber(result)) field.value = null;
                else field.value = result;
            });
        }).catch((err)=>{
            console.error(err);
        });
    }, [
        sourceKeyValue
    ]);
};
const useLazyLoadDisplayAssociationFieldsOfForm_rslib_entry_ = useLazyLoadDisplayAssociationFieldsOfForm;
function hasPreload(record, path) {
    const value = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].get(record, path);
    return null != value && '[{}]' !== JSON.stringify(value) && '{}' !== JSON.stringify(value);
}
export { useLazyLoadDisplayAssociationFieldsOfForm_rslib_entry_ as default };
