'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Behavior = 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 _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 Behavior = exports.Behavior = function (_RuleAttribute) {
    _inherits(Behavior, _RuleAttribute);

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

        _classCallCheck(this, Behavior);

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

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

    _createClass(Behavior, [{
        key: 'process',
        value: function process() {
            // override
        }
    }, {
        key: 'getHeaderName',
        value: function getHeaderName(headerNameOption) {
            return '"' + (this.options[headerNameOption] === 'OTHER' ? this.options.customHeaderName : [headerNameOption]) + '"';
        }
    }, {
        key: 'processRequestMethodOptions',
        value: function processRequestMethodOptions(method, options) {
            return options.enabled ? 'aka_request_method_status["' + method + '"] = "ALLOW"' : 'aka_request_method_status["' + method + '"] = "DENY"';
        }
    }, {
        key: 'processHeaderOptions',
        value: function processHeaderOptions(luaMapName, comment, capture) {
            var headerName = this.switchByVal({
                'MODIFY': this.getHeaderName('standardModifyHeaderName'),
                'ADD': this.getHeaderName('standardAddHeaderName'),
                'REMOVE': this.getHeaderName('standardRemoveHeaderName'),
                'REGEX': this.getHeaderName('standardModifyHeaderName')
            }, '"' + this.options.customHeaderName + '"', this.options.action);

            var headerValue = this.switchByVal({
                'MODIFY': this.value(this.options.newHeaderValue),
                'ADD': this.value(this.options.headerValue),
                'REMOVE': 'nil',
                'REGEX': 'string.gsub(cs(' + luaMapName + '[' + headerName + ']), ' + this.value(this.options.regexHeaderMatch) + ', ' + this.value(this.options.regexHeaderReplace) + ')'
            }, 'nil', this.options.action);

            var lua = [];

            // capture header manipulations to apply after proxy pass as table mapping header to
            // action, value, search, replacement
            if (capture) {
                lua = ['-- ' + this.options.action + ' CAPTURE ' + comment, luaMapName + '[' + headerName + '] = { "' + this.options.action + '", ' + headerValue + ', ' + this.value(this.options.regexHeaderMatch) + ', ' + this.value(this.options.regexHeaderReplace) + ' }'];
            } else {
                lua = ['-- ' + this.options.action + ' ' + comment, luaMapName + '[' + headerName + '] = ' + headerValue];
            }

            return lua;
        }
    }], [{
        key: 'translateTTL',
        value: function translateTTL(ttl) {
            if (!ttl) return 0;
            var ttlSeconds = 0;
            var ttlInt = parseInt(ttl.substring(0, ttl.length - 1));
            switch (ttl.slice(-1)) {
                case 's':
                    ttlSeconds = ttlInt;
                    break;
                case 'm':
                    ttlSeconds = ttlInt * 60;
                    break;
                case 'h':
                    ttlSeconds = ttlInt * 60 * 60;
                    break;
                case 'd':
                    ttlSeconds = ttlInt * 60 * 60 * 24;
                    break;
            }
            return ttlSeconds;
        }
    }]);

    return Behavior;
}(_ruleAttribute.RuleAttribute);

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