import { extractBearerToken } from "../middleware/auth.js";
import { listSessions, listAllSessionsList } from "../../sync/message-store.js";
export async function handleSessionsList(req, res) {
    if (req.method !== "GET") {
        res.statusCode = 405;
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify({ error: "Method Not Allowed" }));
        return true;
    }
    const token = extractBearerToken(req);
    if (!token) {
        res.statusCode = 401;
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify({ error: "Unauthorized" }));
        return true;
    }
    const url = new URL(req.url ?? "/", "http://localhost");
    const deviceId = url.searchParams.get("deviceId")?.trim();
    const sessions = deviceId ? listSessions(deviceId) : listAllSessionsList();
    res.statusCode = 200;
    res.setHeader("Content-Type", "application/json");
    res.end(JSON.stringify({ sessions }));
    return true;
}
