import keytar from 'keytar';
import { createLogger } from '../logger.js';
import { getServerHost } from '../utils.js';
const logger = createLogger('keychain');
const SERVICE_NAME = 'mcpc';
function buildOAuthClientAccountName(serverUrl, profileName) {
    const host = getServerHost(serverUrl);
    return `auth-profile:${host}:${profileName}:client`;
}
function buildOAuthTokensAccountName(serverUrl, profileName) {
    const host = getServerHost(serverUrl);
    return `auth-profile:${host}:${profileName}:tokens`;
}
function buildSessionAccountName(sessionName) {
    return `session:${sessionName}:headers`;
}
function buildProxyBearerTokenAccountName(sessionName) {
    return `session:${sessionName}:proxy-bearer-token`;
}
export async function storeKeychainOAuthClientInfo(serverUrl, profileName, client) {
    const account = buildOAuthClientAccountName(serverUrl, profileName);
    const value = JSON.stringify(client);
    logger.debug(`Storing OAuth client info for ${profileName} @ ${serverUrl}`);
    await keytar.setPassword(SERVICE_NAME, account, value);
}
export async function readKeychainOAuthClientInfo(serverUrl, profileName) {
    const account = buildOAuthClientAccountName(serverUrl, profileName);
    logger.debug(`Retrieving OAuth client info for ${profileName} @ ${serverUrl}`);
    const value = await keytar.getPassword(SERVICE_NAME, account);
    if (!value) {
        return undefined;
    }
    try {
        return JSON.parse(value);
    }
    catch (error) {
        logger.error(`Failed to parse OAuth client info from keychain: ${error.message}`);
        return undefined;
    }
}
export async function removeKeychainOAuthClientInfo(serverUrl, profileName) {
    const account = buildOAuthClientAccountName(serverUrl, profileName);
    logger.debug(`Deleting OAuth client info for ${profileName} @ ${serverUrl}`);
    return keytar.deletePassword(SERVICE_NAME, account);
}
export async function storeKeychainOAuthTokenInfo(serverUrl, profileName, tokens) {
    const account = buildOAuthTokensAccountName(serverUrl, profileName);
    const value = JSON.stringify(tokens);
    logger.debug(`Storing OAuth tokens for ${profileName} @ ${serverUrl}`);
    await keytar.setPassword(SERVICE_NAME, account, value);
}
export async function readKeychainOAuthTokenInfo(serverUrl, profileName) {
    const account = buildOAuthTokensAccountName(serverUrl, profileName);
    logger.debug(`Retrieving OAuth tokens for ${profileName} @ ${serverUrl}`);
    const value = await keytar.getPassword(SERVICE_NAME, account);
    if (!value) {
        return undefined;
    }
    try {
        return JSON.parse(value);
    }
    catch (error) {
        logger.error(`Failed to parse OAuth tokens from keychain: ${error.message}`);
        return undefined;
    }
}
export async function removeKeychainOAuthTokenInfo(serverUrl, profileName) {
    const account = buildOAuthTokensAccountName(serverUrl, profileName);
    logger.debug(`Deleting OAuth tokens for ${profileName} @ ${serverUrl}`);
    return keytar.deletePassword(SERVICE_NAME, account);
}
export async function storeKeychainSessionHeaders(sessionName, headers) {
    const account = buildSessionAccountName(sessionName);
    const value = JSON.stringify(headers);
    logger.debug(`Storing headers for session ${sessionName}`);
    await keytar.setPassword(SERVICE_NAME, account, value);
}
export async function readKeychainSessionHeaders(sessionName) {
    const account = buildSessionAccountName(sessionName);
    logger.debug(`Retrieving headers for session ${sessionName}`);
    const value = await keytar.getPassword(SERVICE_NAME, account);
    if (!value) {
        return undefined;
    }
    try {
        return JSON.parse(value);
    }
    catch (error) {
        logger.error(`Failed to parse headers from keychain: ${error.message}`);
        return undefined;
    }
}
export async function removeKeychainSessionHeaders(sessionName) {
    const account = buildSessionAccountName(sessionName);
    logger.debug(`Deleting headers for session ${sessionName}`);
    return keytar.deletePassword(SERVICE_NAME, account);
}
export async function storeKeychainProxyBearerToken(sessionName, token) {
    const account = buildProxyBearerTokenAccountName(sessionName);
    logger.debug(`Storing proxy bearer token for session ${sessionName}`);
    await keytar.setPassword(SERVICE_NAME, account, token);
}
export async function readKeychainProxyBearerToken(sessionName) {
    const account = buildProxyBearerTokenAccountName(sessionName);
    logger.debug(`Retrieving proxy bearer token for session ${sessionName}`);
    return (await keytar.getPassword(SERVICE_NAME, account)) || undefined;
}
export async function removeKeychainProxyBearerToken(sessionName) {
    const account = buildProxyBearerTokenAccountName(sessionName);
    logger.debug(`Deleting proxy bearer token for session ${sessionName}`);
    return keytar.deletePassword(SERVICE_NAME, account);
}
//# sourceMappingURL=keychain.js.map