/**
 * Loopback capability projection routes.
 *
 * GET /workspaces/{wsId}/capabilities/repositories
 * GET /workspaces/{wsId}/capabilities/onboarding
 * GET /workspaces/{wsId}/capabilities/settings
 *
 * Behind bridge:read scope (via the route table) and workspace authorization.
 */
import { OnboardingProjectionService } from "../capabilities/onboardingProjectionService.js";
import { RepositoryProjectionService } from "../capabilities/repositoryProjectionService.js";
import { SettingsProjectionService } from "../capabilities/settingsProjectionService.js";
import { authorizeWorkspace } from "./authGuards.js";
import { PUBLIC_IDENTIFIER_SCAN_OPTIONS, writeJson } from "./publicResponse.js";
import { firstCapture, matchPath } from "./routeMatching.js";
export async function handleCapabilityRoutes(context) {
    const { app, auth, request, response, url } = context;
    if (request.method !== "GET")
        return false;
    const repoMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/repositories$/);
    if (repoMatch) {
        const workspaceId = firstCapture(repoMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new RepositoryProjectionService(app.localStore);
        writeJson(response, 200, service.listForWorkspace(workspaceId), {
            surface: "workspace.capabilities.repositories",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const obMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/onboarding$/);
    if (obMatch) {
        const workspaceId = firstCapture(obMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new OnboardingProjectionService(app.localStore);
        writeJson(response, 200, service.listForWorkspace(workspaceId), {
            surface: "workspace.capabilities.onboarding",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const settingsMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/settings$/);
    if (settingsMatch) {
        const workspaceId = firstCapture(settingsMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new SettingsProjectionService(app.localStore);
        writeJson(response, 200, service.listForWorkspace(workspaceId), {
            surface: "workspace.capabilities.settings",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    return false;
}
