"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"));
const path_match_1 = require("next/dist/shared/lib/router/utils/path-match");
const prepare_destination_1 = require("next/dist/shared/lib/router/utils/prepare-destination");
const queryString = __importStar(require("querystring"));
const lodash_1 = require("lodash");
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, queryParams, routesManifest) {
    const redirects = routesManifest.redirects;
    for (const redirect of redirects) {
        const matcher = path_match_1.getPathMatch(redirect.source);
        let params = matcher(path);
        if (redirect.has && params) {
            const hasParams = prepare_destination_1.matchHas({
                headers: {},
                cookies: {}
            }, redirect.has, queryParams);
            if (hasParams) {
                Object.assign(params, hasParams);
            }
            else {
                params = false;
            }
        }
        if (params) {
            const destination = matcher_1.compileDestination(redirect.destination, params);
            if (destination) {
                return {
                    redirectPath: destination,
                    statusCode: redirect.statusCode
                };
            }
        }
    }
    return null;
}
exports.getRedirectPath = getRedirectPath;
function createRedirectResponse(uri, originalQueryParams, statusCode) {
    let location;
    if (originalQueryParams) {
        const [uriPath, uriQuery] = uri.split("?");
        const uriQueryParams = queryString.parse(uriQuery);
        const mergedParams = Object.assign(Object.assign({}, originalQueryParams), uriQueryParams);
        location = `${uriPath}${lodash_1.isEmpty(mergedParams) ? "" : `?${queryString.stringify(mergedParams)}`}`;
    }
    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;
