"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__, {
    parseVariables: ()=>parseVariables,
    conditionAnalyses: ()=>conditionAnalyses,
    getTargetField: ()=>getTargetField,
    targetFieldToVariableString: ()=>targetFieldToVariableString,
    getInnermostKeyAndValue: ()=>getInnermostKeyAndValue
});
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const isVariable_js_namespaceObject = require("../../../variables/utils/isVariable.js");
const transformVariableValue_js_namespaceObject = require("../../../variables/utils/transformVariableValue.js");
const external_logic_js_namespaceObject = require("./logic.js");
const parseVariables = (str, ctx)=>{
    var _str_match;
    const regex = /{{(.*?)}}/;
    const matches = null == str ? void 0 : null === (_str_match = str.match) || void 0 === _str_match ? void 0 : _str_match.call(str, regex);
    if (!matches) return str;
    {
        const result = external_lodash_default().get(ctx, matches[1]);
        return external_lodash_default().isFunction(result) ? result() : result;
    }
};
function getInnermostKeyAndValue(obj) {
    if ('object' != typeof obj || null === obj) return null;
    for(const key in obj)if (Object.prototype.hasOwnProperty.call(obj, key)) {
        if ('[object Object]' === Object.prototype.toString.call(obj[key]) && null !== obj[key]) return getInnermostKeyAndValue(obj[key]);
        return {
            key,
            value: obj[key]
        };
    }
    return null;
}
const getTargetField = (obj)=>{
    const keys = getAllKeys(obj);
    const index = (0, external_lodash_namespaceObject.findIndex)(keys, (key, index, keys)=>{
        if (key.includes('$') && index > 0) return true;
    });
    const result = keys.slice(0, index);
    return result;
};
function getAllKeys(obj) {
    const keys = [];
    function traverse(o) {
        Object.keys(o).sort().forEach(function(key) {
            keys.push(key);
            if (o[key] && 'object' == typeof o[key]) traverse(o[key]);
        });
    }
    traverse(obj);
    return keys;
}
const conditionAnalyses = async (param)=>{
    let { ruleGroup, variables, localVariables } = param;
    const type = Object.keys(ruleGroup)[0] || '$and';
    const conditions = ruleGroup[type];
    let results = [];
    for (const condition of conditions){
        if ('$and' in condition || '$or' in condition) return await conditionAnalyses({
            ruleGroup: condition,
            variables,
            localVariables
        });
        const jsonlogic = getInnermostKeyAndValue(condition);
        const operator = null == jsonlogic ? void 0 : jsonlogic.key;
        if (!operator) return true;
        const targetFieldArr = getTargetField(condition);
        const targetVariableName = targetFieldToVariableString(targetFieldArr);
        const localFormRecord = localVariables.find((v)=>'$nForm' === v.name);
        if (null == localFormRecord ? void 0 : localFormRecord.ctx) {
            const targetValue = external_lodash_default().get(localFormRecord.ctx, targetFieldArr);
            const targetCollectionField = await variables.getCollectionField(targetVariableName, localVariables);
            const result = (0, external_logic_js_namespaceObject.getJsonLogic)().apply({
                [operator]: [
                    (0, transformVariableValue_js_namespaceObject.transformVariableValue)(targetValue, {
                        targetCollectionField
                    }),
                    (0, transformVariableValue_js_namespaceObject.transformVariableValue)(jsonlogic.value, {
                        targetCollectionField
                    })
                ]
            });
            results.push(result);
            continue;
        }
        const targetValue = variables.parseVariable(targetVariableName, localVariables);
        const parsingResult = (0, isVariable_js_namespaceObject.isVariable)(null == jsonlogic ? void 0 : jsonlogic.value) ? [
            variables.parseVariable(null == jsonlogic ? void 0 : jsonlogic.value, localVariables),
            targetValue
        ] : [
            null == jsonlogic ? void 0 : jsonlogic.value,
            targetValue
        ];
        try {
            const jsonLogic = (0, external_logic_js_namespaceObject.getJsonLogic)();
            const [value, targetValue] = await Promise.all(parsingResult);
            const targetCollectionField = await variables.getCollectionField(targetVariableName, localVariables);
            const result = jsonLogic.apply({
                [operator]: [
                    (0, transformVariableValue_js_namespaceObject.transformVariableValue)(targetValue, {
                        targetCollectionField
                    }),
                    (0, transformVariableValue_js_namespaceObject.transformVariableValue)(value, {
                        targetCollectionField
                    })
                ]
            });
            results.push(result);
        } catch (error) {
            throw error;
        }
    }
    if ('$and' === type) return (0, external_lodash_namespaceObject.every)(results, (v)=>v);
    return (0, external_lodash_namespaceObject.some)(results, (v)=>v);
};
function targetFieldToVariableString(targetField) {
    return "{{ $nForm.".concat(targetField.join('.'), " }}");
}
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
});
