import { serialize as serializeCookie } from "cookie";
import { ApiResponse, searchParams } from "../request-response";
import { SET_COOKIE_HEADER, cookieSettingOptions } from "../cookies";
const REDIRECT_SEARCH_PARAM = "makeswift-redirect-live";
async function redirectLiveHandler(req, { previewCookieNames }) {
  const params = 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(
      SET_COOKIE_HEADER,
      serializeCookie(name, "", { ...cookieSettingOptions, expires: /* @__PURE__ */ new Date(0), maxAge: 0 })
    );
  });
  return ApiResponse.redirect(destinationPath, { headers });
}
export {
  REDIRECT_SEARCH_PARAM,
  redirectLiveHandler
};
//# sourceMappingURL=redirect-live.js.map