import { requireAuth } from "../security/http-auth.js";
export function registerPreviewRoutes(app, config, deps) {
    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 session = await deps.previewService.refreshSessionCapture(sessionId);
        return { session };
    });
    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 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 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 query = request.query;
        const linkedBrowserSession = deps.browserCollaborationService?.getSessionByPreviewSessionId(sessionId);
        if (linkedBrowserSession?.secretSafe) {
            return { inspect: null, suppressed: true };
        }
        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,
            projectRoot: body.projectRoot ?? null,
            target: body.target ?? null,
            command: body.command ?? null,
            port: typeof body.port === "number" ? body.port : null,
            frameworkHint: body.frameworkHint ?? null,
        });
        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);
        return { ok: stopped };
    });
}
//# sourceMappingURL=preview.js.map