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__record_provider_index_mjs__ from "../../../../record-provider/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_getPath_mjs__ from "../../../../variables/utils/getPath.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_getVariableName_mjs__ from "../../../../variables/utils/getVariableName.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../../../../variables/utils/isVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_transformVariableValue_mjs__ from "../../../../variables/utils/transformVariableValue.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__association_field_util_mjs__ from "../../association-field/util.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__useSpecialCase_mjs__ from "./useSpecialCase.mjs";
const useParseDefaultValue = ()=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
    const recordV2 = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionRecord)();
    const { isInAssignFieldValues, isInSetDefaultValueDialog, isInFormDataTemplate, isInSubTable, isInSubForm } = (0, __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.useFlag)() || {};
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { isSpecialCase, setDefaultValue } = (0, __WEBPACK_EXTERNAL_MODULE__useSpecialCase_mjs__.useSpecialCase)();
    const index = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecordIndex)();
    const findVariable = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (null == fieldSchema.default || isInSetDefaultValueDialog || isInFormDataTemplate || (0, __WEBPACK_EXTERNAL_MODULE__association_field_util_mjs__.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, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(fieldSchema.default) && null == field.value || field.value === fieldSchema.default || forceUpdate)) {
                if ((0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(field.value)) await field.reset({
                    forceClear: true
                });
                field.loading = true;
                const collectionField = !fieldSchema.name.toString().includes('.') && getField(fieldSchema.name);
                const value = (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_transformVariableValue_mjs__.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 = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].debounce(_run, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.DEBOUNCE_WAIT);
        if ((0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(fieldSchema.default)) {
            const variableName = (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_getVariableName_mjs__.getVariableName)(fieldSchema.default);
            const variable = findVariable(variableName);
            if (!variable) return console.error("useParseDefaultValue: can not find variable ".concat(variableName));
            _run();
            const dispose = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.reaction)(()=>{
                const obj = {
                    [variableName]: (null == variable ? void 0 : variable.ctx) || {}
                };
                const path = (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_getPath_mjs__.getPath)(fieldSchema.default);
                const value = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.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;
export { useParseDefaultValue_rslib_entry_ as default };
