'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Criteria = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _requireDir = require('require-dir');

var _requireDir2 = _interopRequireDefault(_requireDir);

var _ruleAttribute = require('./ruleAttribute.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Criteria = exports.Criteria = function (_RuleAttribute) {
    _inherits(Criteria, _RuleAttribute);

    function Criteria(name, options, valueMap) {
        var _ret;

        _classCallCheck(this, Criteria);

        var _this = _possibleConstructorReturn(this, (Criteria.__proto__ || Object.getPrototypeOf(Criteria)).call(this));

        return _ret = Criteria.create(name, options, valueMap), _possibleConstructorReturn(_this, _ret);
    }

    _createClass(Criteria, [{
        key: 'process',


        /**
         * process criteria options into lua expression
         * @param usePattern
         * @param valueSuffix
         * @returns {string}
         */
        value: function process(usePattern, valueSuffix) {
            var _this2 = this;

            if (['EXISTS', 'IS_NOT_EMPTY', 'DOES_NOT_EXIST', 'IS_EMPTY'].includes(this.options.matchOperator)) {

                return this.checkVar + this.matchOperatorCompare() + 'nil' + ' or ' + this.checkVar + this.matchOperatorCompare() + '""';
            } else {

                var conditionArray = [];
                var valueArray = [];

                if (this.options.value) {
                    valueArray = [this.options.value];
                } else if (this.options.values) {
                    valueArray = [].concat(_toConsumableArray(this.options.values));
                }

                valueArray.forEach(function (val) {

                    if (valueSuffix) val = val + valueSuffix;

                    if (usePattern || _this2.options.matchWildcard === true || val.indexOf('*') > -1) {

                        var negate = ['DOES_NOT_MATCH_ONE_OF', 'IS_NOT', 'IS_NOT_ONE_OF'].includes(_this2.options.matchOperator) ? 'not ' : '';

                        conditionArray.push(negate + 'matches(' + _this2.checkVar + ', ' + _this2.value(val) + ')');
                    } else {

                        if (_this2.useNumericComparison()) {
                            conditionArray.push(_this2.checkVar + _this2.matchOperatorCompare() + 'tonumber(' + _this2.value(val) + ')');
                        } else {
                            conditionArray.push(_this2.checkVar + _this2.matchOperatorCompare() + _this2.value(val));
                        }
                    }
                });

                return conditionArray.join(this.matchOperatorJoiner());
            }
        }
    }, {
        key: 'matchOperatorJoiner',
        value: function matchOperatorJoiner() {
            return this.switchByVal({
                'MATCHES_ONE_OF': ' or ',
                'IS_ONE_OF': ' or ',
                'IS_NOT_ONE_OF': ' and ',
                'DOES_NOT_MATCH_ONE_OF': ' and '
            }, ' and ', this.options.matchOperator);
        }
    }, {
        key: 'matchOperatorCompare',
        value: function matchOperatorCompare() {
            return this.switchByVal({
                'EXISTS': ' ~= ',
                'DOES_NOT_EXIST': ' == ',
                'IS': ' == ',
                'IS_ONE_OF': ' == ',
                'IS_NOT': ' ~= ',
                'IS_NOT_ONE_OF': ' ~= ',
                'IS_GREATER_THAN': ' > ',
                'IS_LESS_THAN': ' < ',
                'IS_GREATER_THAN_OR_EQUAL_TO': ' >= ',
                'IS_LESS_THAN_OR_EQUAL_TO': ' <= '
            }, ' == ', this.options.matchOperator);
        }
    }, {
        key: 'useNumericComparison',
        value: function useNumericComparison() {
            return ['IS_GREATER_THAN', 'IS_LESS_THAN', 'IS_GREATER_THAN_OR_EQUAL_TO', 'IS_LESS_THAN_OR_EQUAL_TO'].includes(this.options.matchOperator);
        }
    }], [{
        key: 'checkVar',
        get: function get() {
            return this._checkVar;
        },
        set: function set(checkVar) {
            this._checkVar = checkVar;
        }
    }]);

    return Criteria;
}(_ruleAttribute.RuleAttribute);

(0, _requireDir2.default)('./criteria');