import { isRecord, mapWorkspace } from "../internal/rowMappers.js";
import { clampLimit } from "../internal/cursors.js";
export class WorkspaceRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    upsertWorkspace(input) {
        const now = new Date().toISOString();
        this.#db
            .prepare(`INSERT INTO workspaces (id, root_path, created_at, updated_at)
         VALUES (?, ?, ?, ?)
         ON CONFLICT(id) DO UPDATE SET root_path = excluded.root_path, updated_at = excluded.updated_at`)
            .run(input.id, input.rootPath, now, now);
        const workspace = this.#db
            .prepare("SELECT id, root_path, created_at, updated_at FROM workspaces WHERE id = ?")
            .get(input.id);
        return mapWorkspace(workspace);
    }
    findWorkspace(id) {
        const workspace = this.#db
            .prepare("SELECT id, root_path, created_at, updated_at FROM workspaces WHERE id = ?")
            .get(id);
        return isRecord(workspace) ? mapWorkspace(workspace) : undefined;
    }
    listWorkspaceIds(limit = 200) {
        const rows = this.#db
            .prepare("SELECT id FROM workspaces ORDER BY updated_at DESC, id ASC LIMIT ?")
            .all(clampLimit(limit));
        return rows
            .filter(isRecord)
            .map((row) => String(row.id))
            .filter(Boolean);
    }
}
