import inquirer from "inquirer";
import { logger } from "../utils/logger.js";
import { setToken, removeToken, listStoredProviders, isKeychainAvailable, } from "../core/auth.js";
import { SUPPORTED_PROVIDERS, PROVIDER_DISPLAY_NAMES, PROVIDER_ENV_KEYS, invalidProviderError, } from "../constants.js";
function isValidProvider(provider) {
    return SUPPORTED_PROVIDERS.includes(provider);
}
export async function authSetAction(provider) {
    if (!isValidProvider(provider)) {
        logger.error(invalidProviderError(provider));
        return;
    }
    if (!isKeychainAvailable()) {
        const envKey = PROVIDER_ENV_KEYS[provider];
        logger.error(`OS keychain not available. Use environment variables instead: export ${envKey}=...`);
        return;
    }
    const { token } = await inquirer.prompt([
        {
            type: "password",
            name: "token",
            message: `Enter ${PROVIDER_DISPLAY_NAMES[provider]} API token:`,
            validate: (input) => input.trim().length > 0 || "Token is required",
        },
    ]);
    const trimmed = token.trim();
    const success = setToken(provider, trimmed);
    if (success) {
        logger.success(`${PROVIDER_DISPLAY_NAMES[provider]} token saved to OS keychain.`);
    }
    else {
        logger.error(`Failed to save ${PROVIDER_DISPLAY_NAMES[provider]} token.`);
    }
}
export async function authRemoveAction(provider) {
    if (!isValidProvider(provider)) {
        logger.error(invalidProviderError(provider));
        return;
    }
    const success = removeToken(provider);
    if (success) {
        logger.success(`${PROVIDER_DISPLAY_NAMES[provider]} token removed from OS keychain.`);
    }
    else {
        logger.error(`No token found for ${PROVIDER_DISPLAY_NAMES[provider]} in OS keychain.`);
    }
}
export async function authListAction() {
    const providers = listStoredProviders();
    if (providers.length === 0) {
        logger.info("No tokens stored in OS keychain.");
        return;
    }
    logger.info("Tokens stored in OS keychain:");
    for (const provider of providers) {
        const displayName = PROVIDER_DISPLAY_NAMES[provider] ?? provider;
        logger.success(displayName);
    }
    logger.info("Tokens from environment variables are not listed here.");
}
export function registerAuthCommands(program) {
    const auth = program
        .command("auth")
        .description("Manage provider API tokens (OS keychain)");
    auth
        .command("set <provider>")
        .description("Store a provider API token in OS keychain")
        .action(authSetAction);
    auth
        .command("remove <provider>")
        .description("Remove a provider API token from OS keychain")
        .action(authRemoveAction);
    auth
        .command("list")
        .description("List providers with stored tokens")
        .action(authListAction);
}
//# sourceMappingURL=auth.js.map