import { getMaybeAuthenticationState, getMaybeUser, isUnauthenticated } from "../../authentication/index.js";
import { deleteAuthenticationState, deleteExternalUser } from "../../data/index.js";
import { authenticate } from "../authentication.js";
import { ok } from "../../is.js";
import "@fastify/cookie";
export async function logoutResponse(response) {
    const state = getMaybeAuthenticationState();
    if (state && state.type !== "partner") {
        await deleteAuthenticationState(state.stateId);
        const user = getMaybeUser();
        if (user.externalType === "anonymous") {
            await deleteExternalUser({
                externalId: user.userId,
                externalType: user.externalType
            });
        }
    }
    response.clearCookie("state", {
        path: "/",
        signed: true,
    });
}
export async function logoutRoutes(fastify) {
    fastify.get("/logout", {
        preHandler: authenticate(fastify),
        async handler(request, response) {
            ok(!isUnauthenticated(), "Expected authentication");
            await logoutResponse(response);
            response.header("Location", "/");
            response.status(302);
            response.send("Redirecting");
        },
    });
}
//# sourceMappingURL=logout.js.map