/**
 * Handler for projects_list messages.
 * Returns the list of registered projects from the local SQLite database.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { listProjects } from "../db/repositories/projects.js";
import { logger } from "../utils/logger.js";
export async function handleProjectsList(message, send) {
    const msg = message;
    const { requestId } = msg;
    try {
        const projects = listProjects();
        const projectInfos = projects.map((p) => ({
            projectId: p.project_id,
            name: p.name,
            path: p.path,
            // Active session status comes via SSE events; we don't query for it here
            hasActiveSession: false,
            lastAccessed: p.last_opened_at ?? p.created_at,
        }));
        send({
            type: "projects_list_response",
            requestId,
            payload: {
                projects: projectInfos,
            },
        });
    }
    catch (err) {
        const errorMessage = err instanceof Error ? err.message : String(err);
        logger.error(`projects_list error: ${errorMessage}`);
        send({
            type: "projects_list_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: errorMessage,
            },
        });
    }
}
//# sourceMappingURL=projects-list.js.map