"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSavedAuthAvailability = getSavedAuthAvailability;
const promises_1 = require("node:fs/promises");
const claude_cli_1 = require("./claude-cli");
const credential_store_1 = require("./credential-store");
const kind_1 = require("./kind");
async function getSavedAuthAvailability(profile, credentialStore) {
    if (profile.kind === "codex-env-var" || profile.kind === "claude-env-var") {
        return process.env[profile.envVarName]?.trim()
            ? { available: true }
            : {
                available: false,
                reason: "missing_env_var",
                envVarName: profile.envVarName,
            };
    }
    if (profile.kind === "claude-api-key") {
        try {
            return (0, credential_store_1.readStoredCredential)(profile, credentialStore)
                ? { available: true }
                : { available: false, reason: "missing_stored_credential" };
        }
        catch {
            return { available: false, reason: "auth_unavailable" };
        }
    }
    const managedPath = (0, kind_1.getManagedAuthProfilePath)(profile);
    if (!managedPath) {
        return {
            available: false,
            reason: "missing_managed_sign_in_data",
        };
    }
    try {
        await (0, promises_1.access)(managedPath, promises_1.constants.F_OK);
    }
    catch {
        return {
            available: false,
            reason: "missing_managed_sign_in_data",
        };
    }
    if (profile.kind === "claude-cli") {
        try {
            const status = await (0, claude_cli_1.getClaudeCliAuthStatus)({
                claudeConfigPath: profile.claudeConfigPath,
            });
            return status?.loggedIn
                ? { available: true }
                : { available: false, reason: "auth_unavailable" };
        }
        catch {
            return { available: false, reason: "auth_unavailable" };
        }
    }
    return { available: true };
}
