import { NextResponse } from "next/server";
import { getSecurityEmailBaseUrl } from "@open-mercato/shared/lib/url";
function toRelativeRedirectPath(path) {
  const withoutBackslashes = path.replace(/\\/g, "/");
  const rooted = withoutBackslashes.startsWith("/") ? withoutBackslashes : `/${withoutBackslashes}`;
  return rooted.replace(/^\/+/, "/");
}
function resolveTrustedRedirectBase(req) {
  try {
    return getSecurityEmailBaseUrl(req);
  } catch {
    return null;
  }
}
function resolveSafeRedirectLocation(req, path) {
  const safePath = toRelativeRedirectPath(path);
  const base = resolveTrustedRedirectBase(req);
  if (base) return new URL(safePath, base).toString();
  return safePath;
}
function buildSafeRedirectResponse(req, path, status = 307) {
  return new NextResponse(null, {
    status,
    headers: { Location: resolveSafeRedirectLocation(req, path) }
  });
}
function buildRequestOriginUrl(req, path) {
  return resolveSafeRedirectLocation(req, path);
}
export {
  buildRequestOriginUrl,
  buildSafeRedirectResponse,
  resolveSafeRedirectLocation,
  resolveTrustedRedirectBase
};
//# sourceMappingURL=requestRedirect.js.map
