import * as __WEBPACK_EXTERNAL_MODULE__tachybase_evaluators_client__ from "@tachybase/evaluators/client";
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__schema_settings_LinkageRules_type_mjs__ from "../../../schema-settings/LinkageRules/type.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../../../variables/utils/isVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__ from "../../common/utils/uitls.mjs";
async function getTempFieldState(condition, value) {
    [condition, value] = await Promise.all([
        condition,
        value
    ]);
    return {
        condition,
        value
    };
}
const collectFieldStateOfLinkageRules = (param)=>{
    let { operator, value, field, condition, variables, localVariables } = param;
    var _field_stateOfLinkageRules, _field_initStateOfLinkageRules, _field_stateOfLinkageRules1, _field_initStateOfLinkageRules1, _field_stateOfLinkageRules2, _field_initStateOfLinkageRules2, _field_stateOfLinkageRules3, _field_initStateOfLinkageRules3;
    const requiredResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules ? void 0 : _field_stateOfLinkageRules.required) || [
        null == field ? void 0 : null === (_field_initStateOfLinkageRules = field.initStateOfLinkageRules) || void 0 === _field_initStateOfLinkageRules ? void 0 : _field_initStateOfLinkageRules.required
    ];
    const displayResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules1 = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules1 ? void 0 : _field_stateOfLinkageRules1.display) || [
        null == field ? void 0 : null === (_field_initStateOfLinkageRules1 = field.initStateOfLinkageRules) || void 0 === _field_initStateOfLinkageRules1 ? void 0 : _field_initStateOfLinkageRules1.display
    ];
    const patternResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules2 = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules2 ? void 0 : _field_stateOfLinkageRules2.pattern) || [
        null == field ? void 0 : null === (_field_initStateOfLinkageRules2 = field.initStateOfLinkageRules) || void 0 === _field_initStateOfLinkageRules2 ? void 0 : _field_initStateOfLinkageRules2.pattern
    ];
    const valueResult = (null == field ? void 0 : null === (_field_stateOfLinkageRules3 = field.stateOfLinkageRules) || void 0 === _field_stateOfLinkageRules3 ? void 0 : _field_stateOfLinkageRules3.value) || [
        null == field ? void 0 : null === (_field_initStateOfLinkageRules3 = field.initStateOfLinkageRules) || void 0 === _field_initStateOfLinkageRules3 ? void 0 : _field_initStateOfLinkageRules3.value
    ];
    const { evaluate } = __WEBPACK_EXTERNAL_MODULE__tachybase_evaluators_client__.evaluators.get('formula.js');
    switch(operator){
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Required:
            requiredResult.push(getTempFieldState((0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), true));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                required: requiredResult
            };
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.InRequired:
            requiredResult.push(getTempFieldState((0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), false));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                required: requiredResult
            };
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Visible:
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.None:
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Hidden:
            displayResult.push(getTempFieldState((0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), operator));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                display: displayResult
            };
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Editable:
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.ReadOnly:
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.ReadPretty:
            patternResult.push(getTempFieldState((0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), operator));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                pattern: patternResult
            };
            break;
        case __WEBPACK_EXTERNAL_MODULE__schema_settings_LinkageRules_type_mjs__.ActionType.Value:
            {
                const getValue = async ()=>{
                    if ((null == value ? void 0 : value.mode) === 'express') {
                        if (null == (value.value || value.result)) return;
                        const { exp, scope: expScope } = await replaceVariables(value.value || value.result, {
                            variables,
                            localVariables
                        });
                        try {
                            const result = evaluate(exp, {
                                now: ()=>new Date().toString(),
                                ...expScope
                            });
                            return result;
                        } catch (error) {
                            console.error(error);
                        }
                    } else if ((null == value ? void 0 : value.mode) === 'constant') return (null == value ? void 0 : value.value) || value;
                    else return null;
                };
                if (isConditionEmpty(condition)) valueResult.push(getTempFieldState(true, getValue()));
                else valueResult.push(getTempFieldState((0, __WEBPACK_EXTERNAL_MODULE__common_utils_uitls_mjs__.conditionAnalyses)({
                    ruleGroup: condition,
                    variables,
                    localVariables
                }), getValue()));
                field.stateOfLinkageRules = {
                    ...field.stateOfLinkageRules,
                    value: valueResult
                };
            }
            break;
        default:
            return null;
    }
};
async function replaceVariables(value, param) {
    let { variables, localVariables } = param;
    var _value_match;
    const store = {};
    const scope = {};
    if (null == value) return;
    const waitForParsing = null === (_value_match = value.match(__WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.REGEX_OF_VARIABLE)) || void 0 === _value_match ? void 0 : _value_match.map(async (item)=>{
        const result = await variables.parseVariable(item, localVariables);
        const id = "_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.uid)());
        scope[id] = result;
        store[item] = id;
        return result;
    });
    if (waitForParsing) await Promise.all(waitForParsing);
    return {
        exp: value.replace(__WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.REGEX_OF_VARIABLE, (match)=>"{{".concat(store[match] || match, "}}")),
        scope
    };
}
function isConditionEmpty(rules) {
    const type = Object.keys(rules)[0] || '$and';
    const conditions = rules[type];
    return __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(conditions);
}
export { collectFieldStateOfLinkageRules, getTempFieldState };
