'use strict';

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

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; }; }();

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

var RuleAttribute = function () {
    function RuleAttribute() {
        _classCallCheck(this, RuleAttribute);

        return this;
    }

    _createClass(RuleAttribute, [{
        key: 'switchByVal',
        value: function switchByVal(cases, defaultCase, key) {
            var result = void 0;
            if (key in cases) result = cases[key];

            // if exact key not found try splitting comma delimited and check each subkey
            if (!result) {
                Object.keys(cases).forEach(function (k) {
                    var subKeys = k.split(',').map(function (i) {
                        return i.trim();
                    });
                    if (subKeys.includes(key)) {
                        result = cases[k];
                    }
                });
            }

            if (!result) result = defaultCase;
            return result;
        }
    }, {
        key: 'process',
        value: function process() {
            // override
        }
    }, {
        key: 'value',
        value: function value(_value) {
            var result = void 0;
            if (this.valueMap && this.valueMap.has(_value)) {
                var replacement = this.valueMap.get(_value);
                console.debug('replacing "' + _value + '" with "' + replacement + '"');
                result = '"' + replacement + '"';
            } else {
                result = '"' + _value + '"';
            }

            return 'swapVars(' + result + ')';
        }
    }], [{
        key: 'isRegistered',
        value: function isRegistered(clazzname) {
            return this.registeredTypes.has(clazzname);
        }
    }, {
        key: 'register',
        value: function register(clazzname, clazz) {
            if (!(this.isRegistered(clazzname) && clazz.prototype instanceof this)) {
                this.registeredTypes.set(clazzname, clazz);
                console.info(this.name + ' registered: ' + clazzname);
            } else {
                console.log('invalid type. must be a subclass of ' + this.name);
            }
        }
    }, {
        key: 'create',
        value: function create(clazzname) {
            if (!clazzname) return null;
            if (!this.registeredTypes.has(clazzname)) {
                console.error(this.name + ' not registered: ' + clazzname);
                return null;
            }

            var clazz = this.registeredTypes.get(clazzname);

            for (var _len = arguments.length, options = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
                options[_key - 1] = arguments[_key];
            }

            var instance = new (Function.prototype.bind.apply(clazz, [null].concat(options)))();
            return instance;
        }
    }, {
        key: 'valueMap',
        get: function get() {
            return this._valueMap;
        },
        set: function set(valueMap) {
            this._valueMap = valueMap;
        }
    }, {
        key: 'registeredTypes',
        get: function get() {
            if (this.hasOwnProperty('_registeredTypeMap')) {
                return this._registeredTypeMap;
            } else {
                this._registeredTypeMap = new Map();
                return this._registeredTypeMap;
            }
        }
    }]);

    return RuleAttribute;
}();

exports.RuleAttribute = RuleAttribute;