"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasAuthClearSelector = hasAuthClearSelector;
exports.filterAuthClearProfiles = filterAuthClearProfiles;
exports.getAuthClearDisabledProviders = getAuthClearDisabledProviders;
exports.printClearNotes = printClearNotes;
exports.printCredentialDeleteFailureWarning = printCredentialDeleteFailureWarning;
const kind_1 = require("../auth-profiles/kind");
const store_1 = require("../auth-profiles/store");
const catalog_1 = require("../i18n/catalog");
function hasAuthClearSelector(options) {
    return (options.all === true ||
        options.targetProvider !== undefined ||
        options.invalid === true);
}
async function filterAuthClearProfiles(profiles, options, homeDirectory, getAuthAvailability, credentialStore) {
    let selectedProfiles = [...profiles];
    if (options.targetProvider !== undefined) {
        selectedProfiles = selectedProfiles.filter((profile) => (0, store_1.getAuthProfileProvider)(profile) === options.targetProvider);
    }
    if (selectedProfiles.length === 0) {
        return selectedProfiles;
    }
    if (options.invalid === true) {
        const { states } = await (0, store_1.loadAuthProfileStateFile)(homeDirectory);
        const stateByProfileId = new Map(states.map((state) => [state.profileId, state]));
        const results = await Promise.all(selectedProfiles.map(async (profile) => {
            const state = stateByProfileId.get(profile.id);
            if (state !== undefined && (0, kind_1.isAuthProfileInvalid)(state)) {
                return { profile, clearable: true };
            }
            const availability = await getAuthAvailability(profile, credentialStore);
            return {
                profile,
                clearable: !availability.available &&
                    availability.reason === "missing_stored_credential",
            };
        }));
        selectedProfiles = results
            .filter((result) => result.clearable)
            .map((result) => result.profile);
    }
    return selectedProfiles;
}
function getAuthClearDisabledProviders(profiles, selectedProfiles) {
    const selectedIds = new Set(selectedProfiles.map((profile) => profile.id));
    return ["codex", "claude"].filter((provider) => {
        const providerProfiles = profiles.filter((profile) => (0, store_1.getAuthProfileProvider)(profile) === provider);
        return (providerProfiles.length > 0 &&
            providerProfiles.every((profile) => selectedIds.has(profile.id)));
    });
}
function printClearNotes(locale, profiles) {
    console.log((0, catalog_1.translate)(locale, "auth.command.clear.note.removeSelected"));
    if (profiles.some((profile) => profile.kind === "claude-api-key" ||
        (profile.kind === "codex-home" &&
            profile.codexLoginMethod === "api-key"))) {
        console.log((0, catalog_1.translate)(locale, "auth.command.clear.note.removesStoredApiKey"));
    }
    if (profiles.some((profile) => profile.kind === "codex-env-var" || profile.kind === "claude-env-var")) {
        console.log((0, catalog_1.translate)(locale, "auth.command.clear.note.keepsEnvVar"));
    }
    if (profiles.some((profile) => (profile.kind === "codex-home" &&
        profile.codexLoginMethod !== "api-key") ||
        profile.kind === "claude-cli")) {
        console.log((0, catalog_1.translate)(locale, "auth.command.clear.note.removesStoredData"));
    }
}
function printCredentialDeleteFailureWarning(locale, failureCount) {
    if (failureCount === 0) {
        return;
    }
    console.log((0, catalog_1.translate)(locale, "auth.command.clear.warning.credentialDeleteFailed", {
        count: failureCount,
    }));
}
