"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 redirect_live_exports = {};
__export(redirect_live_exports, {
  REDIRECT_SEARCH_PARAM: () => REDIRECT_SEARCH_PARAM,
  redirectLiveHandler: () => redirectLiveHandler
});
module.exports = __toCommonJS(redirect_live_exports);
var import_cookie = require("cookie");
var import_request_response = require("../request-response");
var import_cookies = require("../cookies");
const REDIRECT_SEARCH_PARAM = "makeswift-redirect-live";
async function redirectLiveHandler(req, { previewCookieNames }) {
  const params = (0, import_request_response.searchParams)(req);
  const redirectDestination = params.get(REDIRECT_SEARCH_PARAM) ?? "/";
  const redirectDestinationUrl = new URL(
    decodeURIComponent(redirectDestination),
    "http://localhost"
  );
  redirectDestinationUrl.searchParams.delete(REDIRECT_SEARCH_PARAM);
  const destinationPath = `${redirectDestinationUrl.pathname}${redirectDestinationUrl.search}${redirectDestinationUrl.hash}`;
  const headers = new Headers();
  previewCookieNames.forEach((name) => {
    headers.append(
      import_cookies.SET_COOKIE_HEADER,
      (0, import_cookie.serialize)(name, "", { ...import_cookies.cookieSettingOptions, expires: /* @__PURE__ */ new Date(0), maxAge: 0 })
    );
  });
  return import_request_response.ApiResponse.redirect(destinationPath, { headers });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  REDIRECT_SEARCH_PARAM,
  redirectLiveHandler
});
//# sourceMappingURL=redirect-live.js.map