"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDomainRedirectPath = exports.createRedirectResponse = exports.getRedirectPath = exports.isTrailingSlashRedirect = void 0;
const matcher_1 = require("./matcher");
const http = __importStar(require("http"));
function isTrailingSlashRedirect(redirect, basePath) {
    if (basePath !== "") {
        return (redirect.statusCode === 308 &&
            ((redirect.source === `${basePath}` &&
                redirect.destination === `${basePath}/`) ||
                (redirect.source === `${basePath}/` &&
                    redirect.destination === `${basePath}`) ||
                (redirect.source === `${basePath}/:path+/` &&
                    redirect.destination === `${basePath}/:path+`) ||
                (redirect.source === `${basePath}/:file((?:[^/]+/)*[^/]+\\.\\w+)/` &&
                    redirect.destination === `${basePath}/:file`) ||
                (redirect.source === `${basePath}/:notfile((?:[^/]+/)*[^/\\.]+)` &&
                    redirect.destination === `${basePath}/:notfile/`)));
    }
    else {
        return (redirect.statusCode === 308 &&
            ((redirect.source === "/:path+/" && redirect.destination === "/:path+") ||
                (redirect.source === "/:path+" &&
                    redirect.destination === "/:path+/") ||
                (redirect.source === "/:file((?:[^/]+/)*[^/]+\\.\\w+)/" &&
                    redirect.destination === "/:file") ||
                (redirect.source === "/:notfile((?:[^/]+/)*[^/\\.]+)" &&
                    redirect.destination === "/:notfile/")));
    }
}
exports.isTrailingSlashRedirect = isTrailingSlashRedirect;
function getRedirectPath(path, routesManifest) {
    const redirects = routesManifest.redirects;
    for (const redirect of redirects) {
        const match = matcher_1.matchPath(path, redirect.source);
        if (match) {
            const compiledDestination = matcher_1.compileDestination(redirect.destination, match.params);
            if (!compiledDestination) {
                return null;
            }
            return {
                redirectPath: compiledDestination,
                statusCode: redirect.statusCode
            };
        }
    }
    return null;
}
exports.getRedirectPath = getRedirectPath;
function createRedirectResponse(uri, querystring, statusCode) {
    let location;
    if (querystring) {
        const [uriPath, uriQuery] = uri.split("?");
        location = `${uriPath}?${querystring}${uriQuery ? `&${uriQuery}` : ""}`;
    }
    else {
        location = uri;
    }
    const status = statusCode.toString();
    const statusDescription = http.STATUS_CODES[status];
    const refresh = statusCode === 308
        ? [
            {
                key: "Refresh",
                value: `0;url=${location}`
            }
        ]
        : [];
    return {
        status: status,
        statusDescription: statusDescription,
        headers: {
            location: [
                {
                    key: "Location",
                    value: location
                }
            ],
            refresh: refresh
        }
    };
}
exports.createRedirectResponse = createRedirectResponse;
function getDomainRedirectPath(request, buildManifest) {
    const hostHeaders = request.headers["host"];
    if (hostHeaders && hostHeaders.length > 0) {
        const host = hostHeaders[0].value;
        const domainRedirects = buildManifest.domainRedirects;
        if (domainRedirects && domainRedirects[host]) {
            return `${domainRedirects[host]}${request.uri}`;
        }
    }
    return null;
}
exports.getDomainRedirectPath = getDomainRedirectPath;
