/**
 * Brain Hub client — sync workspace index + fetch account brain context
 * via coder.vigthoria.io (JWT-authenticated).
 */
export class BrainHubClient {
    apiBase;
    getAuthToken;
    constructor(options) {
        this.apiBase = (options.apiBase || 'https://coder.vigthoria.io').replace(/\/$/, '');
        this.getAuthToken = options.getAuthToken;
    }
    async headers() {
        const headers = { 'Content-Type': 'application/json' };
        const token = await this.getAuthToken();
        if (token) {
            headers.Authorization = `Bearer ${token}`;
        }
        return headers;
    }
    async syncWorkspaceIndex(payload) {
        const headers = await this.headers();
        if (!headers.Authorization) {
            return { ok: false, skipped: true, reason: 'not_authenticated' };
        }
        const resp = await fetch(`${this.apiBase}/api/brain/sync-index`, {
            method: 'POST',
            headers,
            body: JSON.stringify(payload),
        });
        const data = await resp.json().catch(() => ({}));
        if (!resp.ok) {
            return { ok: false, error: data.error || `HTTP ${resp.status}` };
        }
        return data;
    }
    async fetchAccountContext(limit = 25) {
        const headers = await this.headers();
        if (!headers.Authorization) {
            return { ok: false, formattedText: '', memories: [] };
        }
        const resp = await fetch(`${this.apiBase}/api/brain/context?limit=${limit}`, { headers });
        const data = await resp.json().catch(() => ({}));
        if (!resp.ok) {
            return { ok: false, formattedText: '', error: data.error || `HTTP ${resp.status}` };
        }
        return data;
    }
}
