import { requireAuth } from "../security/http-auth.js";
export function registerPreviewRoutes(app, config, deps) {
    const getBrowserSession = (sessionId) => deps.browserCollaborationService?.getSessionByPreviewSessionId(sessionId) ?? null;
    app.get("/api/preview/session/:sessionId", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const { sessionId } = request.params;
        const browserSession = getBrowserSession(sessionId);
        const session = await deps.previewService.refreshSessionCapture(sessionId);
        if (browserSession?.secretSafe && session) {
            return {
                session: { ...session, browserEvents: [], logs: [], metrics: null },
                browserSession,
            };
        }
        return { session, browserSession };
    });
    app.get("/api/preview/screenshot/:sessionId", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const { sessionId } = request.params;
        const browserSession = getBrowserSession(sessionId);
        if (browserSession?.secretSafe) {
            return {
                screenshot: null,
                suppressed: true,
                reason: "Preview capture is suppressed while the linked browser session is marked secret-safe.",
            };
        }
        const screenshot = await deps.previewService.screenshot(sessionId);
        return { screenshot };
    });
    app.get("/api/preview/logs/:sessionId", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const { sessionId } = request.params;
        const browserSession = getBrowserSession(sessionId);
        if (browserSession?.secretSafe) {
            return {
                logs: [],
                suppressed: true,
                reason: "Preview capture is suppressed while the linked browser session is marked secret-safe.",
            };
        }
        const query = request.query;
        const sinceId = query.sinceId ? Number.parseInt(query.sinceId, 10) : 0;
        const logs = deps.previewService.getLogs(sessionId, sinceId);
        return { logs };
    });
    app.get("/api/preview/inspect/:sessionId", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const { sessionId } = request.params;
        const browserSession = getBrowserSession(sessionId);
        if (browserSession?.secretSafe) {
            return {
                inspect: null,
                suppressed: true,
                reason: "Preview capture is suppressed while the linked browser session is marked secret-safe.",
            };
        }
        const query = request.query;
        const inspect = await deps.previewService.inspect(sessionId, typeof query.selector === "string" ? query.selector : undefined);
        return { inspect };
    });
    app.post("/api/preview/start", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const body = (request.body ?? {});
        if (!body.sessionId) {
            return reply.status(400).send({ error: "sessionId is required" });
        }
        const session = await deps.previewService.start({
            sessionId: body.sessionId,
            workspaceRoot: body.workspaceRoot ?? null,
            target: body.target ?? null,
            command: body.command ?? null,
            port: typeof body.port === "number" ? body.port : null,
            frameworkHint: body.frameworkHint ?? null,
        });
        deps.browserCollaborationService?.syncPreviewSession(session, {
            userId: authUser.id,
            workspaceRoot: body.workspaceRoot ?? null,
            mode: body.target ? "shared" : "isolated",
        });
        return { session };
    });
    app.post("/api/preview/restart", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const body = (request.body ?? {});
        if (!body.sessionId) {
            return reply.status(400).send({ error: "sessionId is required" });
        }
        const session = await deps.previewService.restart(body.sessionId);
        if (!session) {
            return reply.status(404).send({ error: "Preview session not found" });
        }
        return { session };
    });
    app.post("/api/preview/stop", async (request, reply) => {
        const authUser = await requireAuth(request, reply, config.jwtSecret);
        if (!authUser)
            return;
        const body = (request.body ?? {});
        if (!body.sessionId) {
            return reply.status(400).send({ error: "sessionId is required" });
        }
        const stopped = await deps.previewService.stop(body.sessionId);
        if (stopped)
            deps.browserCollaborationService?.closePreviewSession(body.sessionId);
        return { ok: stopped };
    });
}
//# sourceMappingURL=preview.js.map