"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__, {
    default: ()=>useLazyLoadDisplayAssociationFieldsOfForm_rslib_entry_
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const index_js_namespaceObject = require("../../../../block-provider/hooks/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../../collection-manager/index.js");
const CollectionRecordProvider_js_namespaceObject = require("../../../../data-source/collection-record/CollectionRecordProvider.js");
const external_flag_provider_index_js_namespaceObject = require("../../../../flag-provider/index.js");
const external_variables_index_js_namespaceObject = require("../../../../variables/index.js");
const transformVariableValue_js_namespaceObject = require("../../../../variables/utils/transformVariableValue.js");
const hooks_js_namespaceObject = require("../../association-field/hooks.js");
const external_utils_js_namespaceObject = require("../utils.js");
const useLazyLoadDisplayAssociationFieldsOfForm = ()=>{
    var _sourceCollectionFieldRef_current;
    const { name } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const form = (0, schema_namespaceObject.useForm)();
    const recordData = (0, CollectionRecordProvider_js_namespaceObject.useCollectionRecordData)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const field = (0, schema_namespaceObject.useField)();
    const { formValue: subFormValue } = (0, hooks_js_namespaceObject.useSubFormValue)();
    const { isInSubForm, isInSubTable } = (0, external_flag_provider_index_js_namespaceObject.useFlag)() || {};
    const { getAssociationAppends } = (0, index_js_namespaceObject.useAssociationNames)();
    const schemaName = fieldSchema.name.toString();
    const formValue = external_lodash_default().cloneDeep(isInSubForm || isInSubTable ? subFormValue : form.values);
    const collectionFieldRef = (0, external_react_namespaceObject.useRef)(null);
    const sourceCollectionFieldRef = (0, external_react_namespaceObject.useRef)(null);
    const hasPreloadData = (0, external_react_namespaceObject.useMemo)(()=>hasPreload(recordData, schemaName), [
        recordData,
        schemaName
    ]);
    if (null == collectionFieldRef.current && (0, external_utils_js_namespaceObject.isDisplayField)(schemaName)) collectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName));
    if (null == sourceCollectionFieldRef.current && (0, external_utils_js_namespaceObject.isDisplayField)(schemaName)) sourceCollectionFieldRef.current = getCollectionJoinField("".concat(name, ".").concat(schemaName.split('.')[0]));
    const sourceKeyValue = (0, external_utils_js_namespaceObject.isDisplayField)(schemaName) && sourceCollectionFieldRef.current ? external_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, external_react_namespaceObject.useEffect)(()=>{
        if (!(0, external_utils_js_namespaceObject.isDisplayField)(schemaName) || !variables || 'fields' === name || !collectionFieldRef.current || hasPreloadData) return;
        if (external_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, client_namespaceObject.nextTick)(()=>{
                const result = (0, transformVariableValue_js_namespaceObject.transformVariableValue)(value, {
                    targetCollectionField: collectionFieldRef.current
                });
                if (external_lodash_default().isEmpty(result) && !external_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 = external_lodash_default().get(record, path);
    return null != value && '[{}]' !== JSON.stringify(value) && '{}' !== JSON.stringify(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
});
