import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { verifyJwt } from "@open-mercato/shared/lib/auth/jwt";
import { buildSafeRedirectResponse } from "@open-mercato/core/modules/auth/lib/requestRedirect";
function parseCookie(req, name) {
  const cookie = req.headers.get("cookie") || "";
  const m = cookie.match(new RegExp("(?:^|;\\s*)" + name + "=([^;]+)"));
  return m ? decodeURIComponent(m[1]) : null;
}
function extractSessionIdFromAuthToken(token) {
  if (!token) return null;
  try {
    const payload = verifyJwt(token);
    if (!payload) return null;
    const sid = payload.sid;
    return typeof sid === "string" && sid.length > 0 ? sid : null;
  } catch {
    return null;
  }
}
async function POST(req) {
  const sessToken = parseCookie(req, "session_token");
  const authToken = parseCookie(req, "auth_token");
  const sessionId = extractSessionIdFromAuthToken(authToken);
  if (sessToken || sessionId) {
    try {
      const c = await createRequestContainer();
      const auth = c.resolve("authService");
      if (sessionId) {
        await auth.deleteSessionById(sessionId);
      }
      if (sessToken) {
        await auth.deleteSessionByToken(sessToken);
      }
    } catch {
    }
  }
  const res = buildSafeRedirectResponse(req, "/login");
  res.cookies.set("auth_token", "", { path: "/", maxAge: 0 });
  res.cookies.set("session_token", "", { path: "/", maxAge: 0 });
  return res;
}
const metadata = {
  POST: { requireAuth: true }
};
const openApi = {
  tag: "Authentication & Accounts",
  summary: "Log out current session",
  methods: {
    POST: {
      summary: "Invalidate session and redirect",
      description: "Clears authentication cookies and redirects the browser to the login page.",
      responses: [
        { status: 302, description: "Redirect to login after successful logout", mediaType: "text/html" }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=logout.js.map
