'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.BehaviorDownstreamCache = 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 _behavior = require('../behavior.js');

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 BehaviorDownstreamCache = exports.BehaviorDownstreamCache = function (_Behavior) {
    _inherits(BehaviorDownstreamCache, _Behavior);

    function BehaviorDownstreamCache(options, valueMap) {
        _classCallCheck(this, BehaviorDownstreamCache);

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

        _this.options = options;
        _this.valueMap = valueMap;
        return _this;
    }

    _createClass(BehaviorDownstreamCache, [{
        key: 'process',
        value: function process() {

            var expires = this.expires();

            var lua = this.switchByVal({
                'ALLOW': ['aka_downstream_headers["Cache-Control"] = {"SET", "max-age=" .. ' + expires + '}', 'aka_downstream_headers["Expires"] = {"SET", expiryDateString(' + expires + ')}'],
                'MUST_REVALIDATE': ['aka_downstream_headers["Cache-Control"] = {"SET", "no-cache"}'],
                'BUST': ['aka_downstream_headers["Cache-Control"] = {"SET", "max-age=0, no-cache, no-store"}', 'aka_downstream_headers["Pragma"] = {"SET", "no-cache"}', 'aka_downstream_headers["Expires"] = {"SET", "1 January, 1970 00:00:01 GMT"}'],
                'TUNNEL_ORIGIN': ['-- let cache headers pass though from origin'],
                'NONE': ['aka_downstream_headers["Cache-Control"] = {"SET", nil}', 'aka_downstream_headers["Expires"] = {"SET", nil}', 'aka_downstream_headers["Pragma"] = {"SET", nil}']
            }, [], this.options.behavior);

            lua = [].concat(_toConsumableArray(lua), _toConsumableArray(this.switchByVal({
                'PASS_ORIGIN': ['-- let cache headers pass though'],
                'CACHE_CONTROL': ['aka_downstream_headers["Expires"] = {"SET", nil}'],
                'EXPIRES': ['aka_downstream_headers["Cache-Control"] = {"SET", nil}'],
                'CACHE_CONTROL_AND_EXPIRES': ['-- let cache headers pass though'],
                'NONE': ['aka_downstream_headers["Cache-Control"] = {"SET", nil}', 'aka_downstream_headers["Expires"] = {"SET", nil}', 'aka_downstream_headers["Pragma"] = {"SET", nil}']
            }, [], this.options.sendHeaders)));

            if (this.options.sendPrivate) {
                lua.push('ngx.header["Cache-Control"] = "private"');
            }

            return lua;
        }
    }, {
        key: 'expires',
        value: function expires() {
            var expires = this.switchByVal({
                'FROM_VALUE': this.constructor.translateTTL(this.options.ttl),
                'FROM_MAX_AGE': 'ngx.var.aka_cache_ttl_seconds',
                'LESSER': 'ngx.var.aka_cache_ttl_seconds',
                'GREATER': 'ngx.var.aka_cache_ttl_seconds',
                'REMAINING_LIFETIME': 'ngx.var.aka_cache_ttl_seconds',
                'PASS_ORIGIN': ''
            }, '', this.options.allowBehavior);

            return expires;
        }
    }]);

    return BehaviorDownstreamCache;
}(_behavior.Behavior);

_behavior.Behavior.register('downstreamCache', BehaviorDownstreamCache);