"use strict";
/*
 * @adonisjs/shield
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.hstsFactory = void 0;
/// <reference path="../adonis-typings/index.ts" />
const helpers_1 = require("@poppinss/utils/build/helpers");
const noop_1 = require("./noop");
const DEFAULT_MAX_AGE = 180 * 24 * 60 * 60;
/**
 * Normalizes the max age to a valid number
 */
function normalizeMaxAge(maxAge) {
    if (maxAge === null || maxAge === undefined) {
        return DEFAULT_MAX_AGE;
    }
    maxAge = (typeof maxAge === 'string' ? helpers_1.string.toMs(maxAge) : maxAge);
    if (maxAge < 0) {
        throw new Error('Max age for "shield.hsts" cannot be a negative value');
    }
    return maxAge;
}
/**
 * Factory function that returns a new function to Add `Strict-Transport-Security`
 * header based upon given user options.
 */
function hstsFactory(options) {
    if (!options.enabled) {
        return noop_1.noop;
    }
    const maxAge = normalizeMaxAge(options.maxAge);
    let value = `max-age=${maxAge}`;
    if (options.includeSubDomains) {
        value += '; includeSubDomains';
    }
    if (options.preload) {
        value += '; preload';
    }
    return function hsts({ response }) {
        response.header('Strict-Transport-Security', value);
    };
}
exports.hstsFactory = hstsFactory;
