/**
 * OutcomesClient — REST client for /api/outcomes/* endpoints.
 *
 * Endpoints are defined in sprint-522:quick-4 (command-center HTTP server).
 * This client is the SDK-side contract: route authors must match these shapes.
 *
 * Auth: Bearer token injected via `getToken()` on every call.
 * Error handling: 4xx responses throw OutcomesApiError.
 * No Zod dependency — types are validated structurally at the call site.
 */
// ─── Error ───────────────────────────────────────────────────────────────────
export class OutcomesApiError extends Error {
    status;
    body;
    constructor(status, body, message) {
        super(message ?? `OutcomesApiError: HTTP ${status}`);
        this.status = status;
        this.body = body;
        this.name = "OutcomesApiError";
    }
}
class OutcomesClientImpl {
    baseUrl;
    getToken;
    constructor(opts) {
        // Normalise base URL: strip trailing slash to simplify path concatenation.
        this.baseUrl = opts.baseUrl.replace(/\/$/, "");
        this.getToken = opts.getToken;
    }
    // ── private helpers ──────────────────────────────────────────────────────
    async request(path, params) {
        const token = await this.getToken();
        const url = new URL(`${this.baseUrl}${path}`);
        if (params !== undefined) {
            for (const [key, value] of Object.entries(params)) {
                if (value !== undefined) {
                    url.searchParams.set(key, String(value));
                }
            }
        }
        const response = await fetch(url.toString(), {
            method: "GET",
            headers: {
                Authorization: `Bearer ${token}`,
                Accept: "application/json",
            },
        });
        if (!response.ok) {
            const body = await response.text().catch(() => "");
            throw new OutcomesApiError(response.status, body, `OutcomesClient: ${response.status} ${response.statusText} — ${path}`);
        }
        return response.json();
    }
    // ── public methods ───────────────────────────────────────────────────────
    async list(filter = {}) {
        return this.request("/api/outcomes", {
            agent_id: filter.agentId,
            type: filter.type,
            from: filter.from,
            to: filter.to,
            limit: filter.limit,
            cursor: filter.cursor,
        });
    }
    async summary(period) {
        return this.request("/api/outcomes/summary", {
            from: period.from,
            to: period.to,
        });
    }
    async cfo(period) {
        return this.request("/api/outcomes/cfo", {
            from: period.from,
            to: period.to,
        });
    }
    async roi(period, opts) {
        return this.request("/api/outcomes/roi", {
            from: period.from,
            to: period.to,
            sort_by: opts?.sortBy,
            limit: opts?.limit,
        });
    }
}
/**
 * Factory — create an OutcomesClient pointed at a Command Center instance.
 *
 * @example
 * const client = createOutcomesClient({
 *   baseUrl: process.env.COMMAND_CENTER_URL,
 *   getToken: () => auth.getAccessToken(),
 * });
 */
export function createOutcomesClient(opts) {
    return new OutcomesClientImpl(opts);
}
//# sourceMappingURL=client.js.map