/**
 * SAC authentication - Read API key from ~/.seawork/auth.json
 *
 * The auth.json file is created by Seawork desktop app after login.
 * Format:
 * {
 *   "token": "api_key_here",
 *   "email": "user@example.com",
 *   "balance": "100",
 *   "base_url": "https://..."
 * }
 */
import { readFileSync, existsSync } from "fs";
import { join } from "path";
import { homedir } from "os";
import { SACError, ExitCode } from "./errors.js";
/**
 * Get the path to the auth.json file
 */
export function getAuthFilePath(seaworkHome) {
    const home = seaworkHome || process.env.SEAWORK_HOME || join(homedir(), ".seawork");
    return join(home, "auth.json");
}
/**
 * Load auth config from ~/.seawork/auth.json
 */
export function loadAuthConfig(seaworkHome) {
    const authPath = getAuthFilePath(seaworkHome);
    if (!existsSync(authPath)) {
        return {};
    }
    try {
        const content = readFileSync(authPath, "utf-8");
        const parsed = JSON.parse(content);
        return {
            token: typeof parsed.token === "string" ? parsed.token : undefined,
            email: typeof parsed.email === "string" ? parsed.email : undefined,
            balance: typeof parsed.balance === "string" ? parsed.balance : undefined,
            base_url: typeof parsed.base_url === "string" ? parsed.base_url : undefined,
        };
    }
    catch {
        return {};
    }
}
/**
 * Get SAC API key from environment or auth.json
 * Priority: SAC_API_KEY env > SEAART_API_KEY env > ~/.seawork/auth.json token
 */
export function getSACApiKey(seaworkHome) {
    // Environment variables take precedence
    const envKey = process.env.SAC_API_KEY || process.env.SEAART_API_KEY;
    if (envKey) {
        return envKey;
    }
    // Fall back to auth.json
    const auth = loadAuthConfig(seaworkHome);
    return auth.token;
}
/**
 * Default multimodal gateway URL (fallback if auth.json has no base_url)
 */
const DEFAULT_MULTIMODAL_BASE_URL = "https://openresty-gateway.api.seaart.ai/model";
/**
 * Convert LLM gateway URL to multimodal gateway URL
 * e.g., "https://example.com/llm" -> "https://example.com/model"
 */
function toMultimodalUrl(llmBaseUrl) {
    // Replace /llm suffix with /model
    if (llmBaseUrl.endsWith("/llm")) {
        return llmBaseUrl.slice(0, -4) + "/model";
    }
    // If no /llm suffix, append /model
    return llmBaseUrl.replace(/\/$/, "") + "/model";
}
/**
 * Get SAC multimodal base URL
 * Priority: SAC_MULTIMODAL_BASE_URL env > derived from auth.json base_url > default
 */
export function getSACBaseUrl(seaworkHome) {
    // Environment variable takes precedence
    const envUrl = process.env.SAC_MULTIMODAL_BASE_URL;
    if (envUrl) {
        return envUrl;
    }
    // Derive from auth.json base_url (LLM gateway -> multimodal gateway)
    const auth = loadAuthConfig(seaworkHome);
    if (auth.base_url) {
        return toMultimodalUrl(auth.base_url);
    }
    return DEFAULT_MULTIMODAL_BASE_URL;
}
/**
 * Get SAC API key or throw an error if not found
 */
export function requireSACApiKey(seaworkHome) {
    const apiKey = getSACApiKey(seaworkHome);
    if (!apiKey) {
        throw new SACError("No SAC API key found.", ExitCode.AUTH, "Please login via Seawork app first, or set SAC_API_KEY environment variable.");
    }
    return apiKey;
}
//# sourceMappingURL=auth.js.map