"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__, {
    getTempFieldState: ()=>getTempFieldState,
    collectFieldStateOfLinkageRules: ()=>collectFieldStateOfLinkageRules
});
const client_namespaceObject = require("@tachybase/evaluators/client");
const utils_client_namespaceObject = require("@tachybase/utils/client");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const type_js_namespaceObject = require("../../../schema-settings/LinkageRules/type.js");
const isVariable_js_namespaceObject = require("../../../variables/utils/isVariable.js");
const uitls_js_namespaceObject = require("../../common/utils/uitls.js");
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 } = client_namespaceObject.evaluators.get('formula.js');
    switch(operator){
        case type_js_namespaceObject.ActionType.Required:
            requiredResult.push(getTempFieldState((0, uitls_js_namespaceObject.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), true));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                required: requiredResult
            };
            break;
        case type_js_namespaceObject.ActionType.InRequired:
            requiredResult.push(getTempFieldState((0, uitls_js_namespaceObject.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), false));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                required: requiredResult
            };
            break;
        case type_js_namespaceObject.ActionType.Visible:
        case type_js_namespaceObject.ActionType.None:
        case type_js_namespaceObject.ActionType.Hidden:
            displayResult.push(getTempFieldState((0, uitls_js_namespaceObject.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), operator));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                display: displayResult
            };
            break;
        case type_js_namespaceObject.ActionType.Editable:
        case type_js_namespaceObject.ActionType.ReadOnly:
        case type_js_namespaceObject.ActionType.ReadPretty:
            patternResult.push(getTempFieldState((0, uitls_js_namespaceObject.conditionAnalyses)({
                ruleGroup: condition,
                variables,
                localVariables
            }), operator));
            field.stateOfLinkageRules = {
                ...field.stateOfLinkageRules,
                pattern: patternResult
            };
            break;
        case type_js_namespaceObject.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, uitls_js_namespaceObject.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(isVariable_js_namespaceObject.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, utils_client_namespaceObject.uid)());
        scope[id] = result;
        store[item] = id;
        return result;
    });
    if (waitForParsing) await Promise.all(waitForParsing);
    return {
        exp: value.replace(isVariable_js_namespaceObject.REGEX_OF_VARIABLE, (match)=>"{{".concat(store[match] || match, "}}")),
        scope
    };
}
function isConditionEmpty(rules) {
    const type = Object.keys(rules)[0] || '$and';
    const conditions = rules[type];
    return external_lodash_default().isEmpty(conditions);
}
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
});
