"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: ()=>useParseDefaultValue_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("../../../../record-provider/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 getPath_js_namespaceObject = require("../../../../variables/utils/getPath.js");
const getVariableName_js_namespaceObject = require("../../../../variables/utils/getVariableName.js");
const isVariable_js_namespaceObject = require("../../../../variables/utils/isVariable.js");
const transformVariableValue_js_namespaceObject = require("../../../../variables/utils/transformVariableValue.js");
const util_js_namespaceObject = require("../../association-field/util.js");
const external_useSpecialCase_js_namespaceObject = require("./useSpecialCase.js");
const useParseDefaultValue = ()=>{
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const variables = (0, external_variables_index_js_namespaceObject.useVariables)();
    const localVariables = (0, external_variables_index_js_namespaceObject.useLocalVariables)();
    const recordV2 = (0, CollectionRecordProvider_js_namespaceObject.useCollectionRecord)();
    const { isInAssignFieldValues, isInSetDefaultValueDialog, isInFormDataTemplate, isInSubTable, isInSubForm } = (0, external_flag_provider_index_js_namespaceObject.useFlag)() || {};
    const { getField } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const { isSpecialCase, setDefaultValue } = (0, external_useSpecialCase_js_namespaceObject.useSpecialCase)();
    const index = (0, index_js_namespaceObject.useRecordIndex)();
    const findVariable = (0, external_react_namespaceObject.useCallback)((name)=>{
        let result = null == variables ? void 0 : variables.getVariable(name);
        if (!result) result = localVariables.find((item)=>item.name === name);
        return result;
    }, [
        localVariables,
        variables
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (null == fieldSchema.default || isInSetDefaultValueDialog || isInFormDataTemplate || (0, util_js_namespaceObject.isSubMode)(fieldSchema) || !(null == recordV2 ? void 0 : recordV2.isNew) && !isInAssignFieldValues) return;
        const _run = async function() {
            let { forceUpdate = false } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
            if (variables && field && ((0, isVariable_js_namespaceObject.isVariable)(fieldSchema.default) && null == field.value || field.value === fieldSchema.default || forceUpdate)) {
                if ((0, isVariable_js_namespaceObject.isVariable)(field.value)) await field.reset({
                    forceClear: true
                });
                field.loading = true;
                const collectionField = !fieldSchema.name.toString().includes('.') && getField(fieldSchema.name);
                const value = (0, transformVariableValue_js_namespaceObject.transformVariableValue)(await variables.parseVariable(fieldSchema.default, localVariables), {
                    targetCollectionField: collectionField
                });
                if (null == value || '' === value) {
                    field.setInitialValue(null);
                    await field.reset({
                        forceClear: true
                    });
                } else if (isSpecialCase()) {
                    if (0 === index) setDefaultValue(value);
                } else field.setInitialValue(value);
                field.loading = false;
            } else if (!/\{\{.+\}\}/g.test(fieldSchema.default) && field.setInitialValue) field.setInitialValue(fieldSchema.default);
        };
        const run = external_lodash_default().debounce(_run, external_variables_index_js_namespaceObject.DEBOUNCE_WAIT);
        if ((0, isVariable_js_namespaceObject.isVariable)(fieldSchema.default)) {
            const variableName = (0, getVariableName_js_namespaceObject.getVariableName)(fieldSchema.default);
            const variable = findVariable(variableName);
            if (!variable) return console.error("useParseDefaultValue: can not find variable ".concat(variableName));
            _run();
            const dispose = (0, schema_namespaceObject.reaction)(()=>{
                const obj = {
                    [variableName]: (null == variable ? void 0 : variable.ctx) || {}
                };
                const path = (0, getPath_js_namespaceObject.getPath)(fieldSchema.default);
                const value = (0, client_namespaceObject.getValuesByPath)(obj, path);
                if (void 0 === value) return Math.random();
                return value;
            }, ()=>run({
                    forceUpdate: true
                }));
            return dispose;
        }
        if (null == field.value && (isInSubTable || isInSubForm)) field.setValue(fieldSchema.default);
    }, [
        fieldSchema.default,
        localVariables
    ]);
};
const useParseDefaultValue_rslib_entry_ = useParseDefaultValue;
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
});
