"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pages_router_redirect_preview_exports = {};
__export(pages_router_redirect_preview_exports, {
  pagesRouterRedirectPreviewHandler: () => pagesRouterRedirectPreviewHandler
});
module.exports = __toCommonJS(pages_router_redirect_preview_exports);
var import_set_cookie_parser = require("set-cookie-parser");
var import_cookie = require("cookie");
var import_cookies = require("../../../api-handler/cookies");
var import_preview = require("../preview");
var import_site_version = require("../../../api/site-version");
async function pagesRouterRedirectPreviewHandler(req, res, client) {
  const pathname = req.query[import_preview.RewriteRuleMatches.OriginalPath];
  if (pathname == null) {
    return res.status(400).send("Bad request: incoming request does not have an associated pathname");
  }
  const previewToken = req.query[import_preview.SearchParams.PreviewToken];
  if (previewToken == null) {
    return res.status(400).send("Bad request: no preview token provided");
  }
  const verificationResult = await client.readPreviewToken(previewToken);
  if (verificationResult == null) {
    return res.status(401).send("Failed to verify preview token");
  }
  const { payload } = verificationResult;
  const siteVersion = { version: payload.version, token: previewToken };
  const secondsUntilExpiration = (0, import_site_version.secondsUntilSiteVersionExpiration)(siteVersion);
  const serializedSiteVersion = (0, import_site_version.serializeSiteVersion)(siteVersion);
  const setCookie = res.setPreviewData({ siteVersion: serializedSiteVersion }).getHeader(import_cookies.SET_COOKIE_HEADER);
  res.removeHeader(import_cookies.SET_COOKIE_HEADER);
  const parsedCookies = (0, import_set_cookie_parser.parse)(Array.isArray(setCookie) ? setCookie : "");
  const prerenderBypassCookie = parsedCookies.find((c) => c.name === import_preview.PRERENDER_BYPASS_COOKIE);
  const previewDataCookie = parsedCookies.find((c) => c.name === import_preview.PREVIEW_DATA_COOKIE);
  if (prerenderBypassCookie?.value == null || previewDataCookie?.value == null) {
    return res.status(500).send("Could not retrieve preview mode cookies");
  }
  const patchedCookies = [prerenderBypassCookie, previewDataCookie].map(({ name, value }) => {
    return (0, import_cookie.serialize)(name, value, { ...import_cookies.cookieSettingOptions, maxAge: secondsUntilExpiration });
  });
  res.setHeader(import_cookies.SET_COOKIE_HEADER, patchedCookies);
  const destinationUrl = new URL(pathname, "http://test.com");
  destinationUrl.searchParams.delete(import_preview.SearchParams.PreviewToken);
  destinationUrl.searchParams.delete(import_preview.RewriteRuleMatches.PreviewToken);
  destinationUrl.searchParams.delete(import_preview.RewriteRuleMatches.OriginalPath);
  res.redirect(`${destinationUrl.pathname}?${destinationUrl.searchParams.toString()}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  pagesRouterRedirectPreviewHandler
});
//# sourceMappingURL=pages-router-redirect-preview.js.map