import * as __WEBPACK_EXTERNAL_MODULE_dayjs__ from "dayjs";
function getJsonLogic() {
    'use strict';
    if (!Array.isArray) Array.isArray = function(arg) {
        return '[object Array]' === Object.prototype.toString.call(arg);
    };
    function arrayUnique(array) {
        var a = [];
        for(var i = 0, l = array.length; i < l; i++)if (-1 === a.indexOf(array[i])) a.push(array[i]);
        return a;
    }
    function areArraysEqual(arr1, arr2) {
        return JSON.stringify(arr1) === JSON.stringify(arr2);
    }
    var jsonLogic = {};
    var operations = {
        $is: function(a, b) {
            return a === b;
        },
        $match: function(a, b) {
            if (Array.isArray(a) && Array.isArray(b) && a.some((element)=>Array.isArray(element))) return a.some((subArray)=>(null == subArray ? void 0 : subArray.length) === b.length && (null == subArray ? void 0 : subArray.every((element, index)=>element === b[index])));
            return JSON.stringify(a) === JSON.stringify(b);
        },
        $eq: function(a, b) {
            if (Array.isArray(a) && Array.isArray(b)) return areArraysEqual(a, b);
            if (Array.isArray(a)) return a.includes(b);
            return a === b;
        },
        $ne: function(a, b) {
            return a != b;
        },
        '!==': function(a, b) {
            return a !== b;
        },
        $gt: function(a, b) {
            if (Array.isArray(a)) return a.some((k)=>k > b);
            return a > b;
        },
        $gte: function(a, b) {
            return a >= b;
        },
        $lt: function(a, b, c) {
            if (Array.isArray(a)) return a.some((k)=>k < b);
            return void 0 === c ? a < b : a < b && b < c;
        },
        $lte: function(a, b, c) {
            return void 0 === c ? a <= b : a <= b && b <= c;
        },
        $exists: function(a) {
            return jsonLogic.truthy(a);
        },
        $notEmpty: function(a) {
            return jsonLogic.truthy(a);
        },
        $empty: function(a) {
            if (Array.isArray(a)) return a.some((k)=>!jsonLogic.truthy(k));
            return !jsonLogic.truthy(a);
        },
        $notExists: function(a) {
            return !jsonLogic.truthy(a);
        },
        '%': function(a, b) {
            return a % b;
        },
        log: function(a) {
            console.log(a);
            return a;
        },
        $in: function(a, b) {
            if (!b || void 0 === b.indexOf) return false;
            if (Array.isArray(a) && Array.isArray(b)) return b.some((elementB)=>a.includes(elementB));
            return -1 !== b.indexOf(a);
        },
        $notIn: function(a, b) {
            if (!b || void 0 === b.indexOf) return false;
            return !(-1 !== b.indexOf(a));
        },
        $includes: function(a, b) {
            if (!a || void 0 === a.indexOf) return false;
            if (Array.isArray(a)) return a.some((element)=>null == element ? void 0 : element.includes(b));
            return -1 !== a.indexOf(b);
        },
        $notIncludes: function(a, b) {
            if (!a || void 0 === a.indexOf) return false;
            if (Array.isArray(a)) return !a.some((element)=>element.includes(b));
            return !(-1 !== a.indexOf(b));
        },
        $anyOf: function(a, b) {
            if (0 === a.length) return false;
            if (Array.isArray(a) && Array.isArray(b) && a.some((element)=>Array.isArray(element))) return a.some((subArray)=>subArray.some((element)=>b.includes(element)));
            return a.some((element)=>b.includes(element));
        },
        $noneOf: function(a, b) {
            if (0 === a.length) return true;
            if (Array.isArray(a) && Array.isArray(b) && a.some((element)=>Array.isArray(element))) return a.some((subArray)=>subArray.every((element)=>!b.some((bElement)=>element.includes(bElement))));
            return b.some((item)=>!a.includes(item));
        },
        $notMatch: function(a, b) {
            if (a.length !== b.length) return true;
            for(let i = 0; i < a.length; i++)if (a[i] !== b[i]) return true;
            return false;
        },
        $dateOn: function(a, b) {
            if (!a || !b) return false;
            if (!Array.isArray(a)) a = [
                a,
                a
            ];
            if (!Array.isArray(b)) b = [
                b,
                b
            ];
            a = a.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            b = b.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            return a[0].isBetween(b[0], b[1], null, '[]') && a[1].isBetween(b[0], b[1], null, '[]');
        },
        $dateBefore: function(a, b) {
            if (!a || !b) return false;
            if (!Array.isArray(a)) a = [
                a,
                a
            ];
            if (!Array.isArray(b)) b = [
                b,
                b
            ];
            a = a.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            b = b.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            return a[0].isBefore(b[0]) && a[1].isBefore(b[0]);
        },
        $dateNotBefore: function(a, b) {
            return !operations.$dateBefore(a, b);
        },
        $dateAfter: function(a, b) {
            if (!a || !b) return false;
            if (!Array.isArray(a)) a = [
                a,
                a
            ];
            if (!Array.isArray(b)) b = [
                b,
                b
            ];
            a = a.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            b = b.map((date)=>(0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date));
            return a[0].isAfter(b[1]) && a[1].isAfter(b[1]);
        },
        $dateNotAfter: function(a, b) {
            return !operations.$dateAfter(a, b);
        },
        $dateBetween: function(a, b) {
            return operations.$dateOn(a, b);
        },
        $dateNotOn: function(a, b) {
            return !operations.$dateOn(a, b);
        },
        $isTruly: function(a) {
            if (Array.isArray(a)) return a.some((k)=>true === k || 1 === k);
            return true === a || 1 === a;
        },
        $isFalsy: function(a) {
            if (Array.isArray(a)) return a.some((k)=>!jsonLogic.truthy(k));
            return !jsonLogic.truthy(a);
        },
        cat: function() {
            return Array.prototype.join.call(arguments, '');
        },
        substr: function(source, start, end) {
            if (end < 0) {
                var temp = String(source).substr(start);
                return temp.substr(0, temp.length + end);
            }
            return String(source).substr(start, end);
        },
        '+': function() {
            return Array.prototype.reduce.call(arguments, function(a, b) {
                return parseFloat(a, 10) + parseFloat(b, 10);
            }, 0);
        },
        '*': function() {
            return Array.prototype.reduce.call(arguments, function(a, b) {
                return parseFloat(a, 10) * parseFloat(b, 10);
            });
        },
        '-': function(a, b) {
            if (void 0 === b) return -a;
            return a - b;
        },
        '/': function(a, b) {
            return a / b;
        },
        min: function() {
            return Math.min.apply(this, arguments);
        },
        max: function() {
            return Math.max.apply(this, arguments);
        },
        merge: function() {
            return Array.prototype.reduce.call(arguments, function(a, b) {
                return a.concat(b);
            }, []);
        },
        var: function(a, b) {
            var not_found = void 0 === b ? null : b;
            var data = this;
            if (void 0 === a || '' === a || null === a) return data;
            var sub_props = String(a).split('.');
            for(var i = 0; i < sub_props.length; i++){
                if (null == data) return not_found;
                data = data[sub_props[i]];
                if (void 0 === data) return not_found;
            }
            return data;
        },
        missing: function() {
            var missing = [];
            var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments;
            for(var i = 0; i < keys.length; i++){
                var key = keys[i];
                var value = jsonLogic.apply({
                    var: key
                }, this);
                if (null === value || '' === value) missing.push(key);
            }
            return missing;
        },
        missing_some: function(need_count, options) {
            var are_missing = jsonLogic.apply({
                missing: options
            }, this);
            if (options.length - are_missing.length >= need_count) return [];
            return are_missing;
        }
    };
    jsonLogic.is_logic = function(logic) {
        return 'object' == typeof logic && null !== logic && !Array.isArray(logic) && 1 === Object.keys(logic).length;
    };
    jsonLogic.truthy = function(value) {
        if (Array.isArray(value) && 0 === value.length) return false;
        return !!value;
    };
    jsonLogic.get_operator = function(logic) {
        return Object.keys(logic)[0];
    };
    jsonLogic.get_values = function(logic) {
        return logic[jsonLogic.get_operator(logic)];
    };
    jsonLogic.apply = function(logic, data) {
        if (Array.isArray(logic)) return logic.map(function(l) {
            return jsonLogic.apply(l, data);
        });
        if (!jsonLogic.is_logic(logic)) return logic;
        var op = jsonLogic.get_operator(logic);
        var values = logic[op];
        var i;
        var current;
        var scopedLogic;
        var scopedData;
        var initial;
        if (!Array.isArray(values)) values = [
            values
        ];
        if ('if' === op || '?:' == op) {
            for(i = 0; i < values.length - 1; i += 2)if (jsonLogic.truthy(jsonLogic.apply(values[i], data))) return jsonLogic.apply(values[i + 1], data);
            if (values.length === i + 1) return jsonLogic.apply(values[i], data);
            return null;
        }
        if ('$and' === op) {
            for(i = 0; i < values.length; i += 1){
                current = jsonLogic.apply(values[i], data);
                if (!jsonLogic.truthy(current)) break;
            }
            return current;
        }
        if ('or' === op) {
            for(i = 0; i < values.length; i += 1){
                current = jsonLogic.apply(values[i], data);
                if (jsonLogic.truthy(current)) break;
            }
            return current;
        } else if ('filter' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            if (!Array.isArray(scopedData)) return [];
            return scopedData.filter(function(datum) {
                return jsonLogic.truthy(jsonLogic.apply(scopedLogic, datum));
            });
        } else if ('map' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            if (!Array.isArray(scopedData)) return [];
            return scopedData.map(function(datum) {
                return jsonLogic.apply(scopedLogic, datum);
            });
        } else if ('reduce' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            initial = void 0 !== values[2] ? values[2] : null;
            if (!Array.isArray(scopedData)) return initial;
            return scopedData.reduce(function(accumulator, current) {
                return jsonLogic.apply(scopedLogic, {
                    current: current,
                    accumulator: accumulator
                });
            }, initial);
        } else if ('all' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            if (!Array.isArray(scopedData) || !scopedData.length) return false;
            for(i = 0; i < scopedData.length; i += 1)if (!jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return false;
            return true;
        } else if ('none' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            if (!Array.isArray(scopedData) || !scopedData.length) return true;
            for(i = 0; i < scopedData.length; i += 1)if (jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return false;
            return true;
        } else if ('some' === op) {
            scopedData = jsonLogic.apply(values[0], data);
            scopedLogic = values[1];
            if (!Array.isArray(scopedData) || !scopedData.length) return false;
            for(i = 0; i < scopedData.length; i += 1)if (jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return true;
            return false;
        }
        values = values.map(function(val) {
            return jsonLogic.apply(val, data);
        });
        if (operations.hasOwnProperty(op) && 'function' == typeof operations[op]) return operations[op].apply(data, values);
        if (op.indexOf('.') > 0) {
            var sub_ops = String(op).split('.');
            var operation = operations;
            for(i = 0; i < sub_ops.length; i++){
                if (!operation.hasOwnProperty(sub_ops[i])) throw new Error('Unrecognized operation ' + op + ' (failed at ' + sub_ops.slice(0, i + 1).join('.') + ')');
                operation = operation[sub_ops[i]];
            }
            return operation.apply(data, values);
        }
        throw new Error('Unrecognized operation ' + op);
    };
    jsonLogic.uses_data = function(logic) {
        var collection = [];
        if (jsonLogic.is_logic(logic)) {
            var op = jsonLogic.get_operator(logic);
            var values = logic[op];
            if (!Array.isArray(values)) values = [
                values
            ];
            if ('var' === op) collection.push(values[0]);
            else values.forEach(function(val) {
                collection.push.apply(collection, jsonLogic.uses_data(val));
            });
        }
        return arrayUnique(collection);
    };
    jsonLogic.add_operation = function(name, code) {
        operations[name] = code;
    };
    jsonLogic.rm_operation = function(name) {
        delete operations[name];
    };
    jsonLogic.rule_like = function(rule, pattern) {
        if (pattern === rule) return true;
        if ('@' === pattern) return true;
        if ('number' === pattern) return 'number' == typeof rule;
        if ('string' === pattern) return 'string' == typeof rule;
        if ('array' === pattern) return Array.isArray(rule) && !jsonLogic.is_logic(rule);
        if (jsonLogic.is_logic(pattern)) {
            if (jsonLogic.is_logic(rule)) {
                var pattern_op = jsonLogic.get_operator(pattern);
                var rule_op = jsonLogic.get_operator(rule);
                if ('@' === pattern_op || pattern_op === rule_op) return jsonLogic.rule_like(jsonLogic.get_values(rule, false), jsonLogic.get_values(pattern, false));
            }
            return false;
        }
        if (Array.isArray(pattern)) {
            if (Array.isArray(rule)) {
                if (pattern.length !== rule.length) return false;
                for(var i = 0; i < pattern.length; i += 1)if (!jsonLogic.rule_like(rule[i], pattern[i])) return false;
                return true;
            }
        }
        return false;
    };
    return jsonLogic;
}
export { getJsonLogic };
