// storage/oauth-store.ts — OAuth token persistence (SPEC-440)
import { join } from 'node:path';
import { hashProjectPath, readJson, writeJson } from './base-store.js';
function tokensPath(projectPath) {
    const id = hashProjectPath(projectPath);
    return join('data', 'projects', id, 'oauth', 'tokens.json');
}
/**
 * Retrieve the stored OAuth token for a specific integration, or null if not stored.
 */
export async function getOAuthToken(projectPath, integration) {
    const tokens = await readJson(tokensPath(projectPath), []);
    return tokens.find((t) => t.integration === integration) ?? null;
}
/**
 * Persist an OAuth token for a project, replacing any existing token for the same integration.
 */
export async function saveOAuthToken(projectPath, token) {
    const path = tokensPath(projectPath);
    const tokens = await readJson(path, []);
    const index = tokens.findIndex((t) => t.integration === token.integration);
    if (index >= 0) {
        tokens[index] = token;
    }
    else {
        tokens.push(token);
    }
    await writeJson(path, tokens);
}
/**
 * Remove the stored OAuth token for a specific integration.
 */
export async function removeOAuthToken(projectPath, integration) {
    const path = tokensPath(projectPath);
    const tokens = await readJson(path, []);
    const filtered = tokens.filter((t) => t.integration !== integration);
    await writeJson(path, filtered);
}
/**
 * List all stored OAuth tokens for a project.
 */
export async function listOAuthTokens(projectPath) {
    return readJson(tokensPath(projectPath), []);
}
//# sourceMappingURL=oauth-store.js.map