"use strict";
/**
 * Account query and search operations
 * Finding, filtering, and retrieving account information
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllAccountsSummary = exports.isAccountPaused = exports.getActiveAccounts = exports.findAccountByQuery = exports.getAccount = exports.getDefaultAccount = exports.getProviderAccounts = void 0;
const profile_detector_1 = require("../../auth/profile-detector");
const registry_1 = require("./registry");
/**
 * Get all accounts for a provider
 */
function getProviderAccounts(provider) {
    const registry = (0, registry_1.loadAccountsRegistry)();
    // Hydrate the in-memory view from token files without mutating disk on read.
    (0, registry_1.hydrateRegistryFromTokenFiles)(registry);
    const providerAccounts = registry.providers[provider];
    if (!providerAccounts) {
        return [];
    }
    return Object.entries(providerAccounts.accounts).map(([id, meta]) => ({
        id,
        provider,
        isDefault: id === providerAccounts.default,
        ...meta,
    }));
}
exports.getProviderAccounts = getProviderAccounts;
/**
 * Get default account for a provider
 */
function getDefaultAccount(provider) {
    const accounts = getProviderAccounts(provider);
    return accounts.find((a) => a.isDefault) || accounts[0] || null;
}
exports.getDefaultAccount = getDefaultAccount;
/**
 * Get specific account by ID
 */
function getAccount(provider, accountId) {
    const accounts = getProviderAccounts(provider);
    return accounts.find((a) => a.id === accountId) || null;
}
exports.getAccount = getAccount;
/**
 * Find account by query (nickname, email, or id)
 * Supports partial matching for convenience
 */
function findAccountByQuery(provider, query) {
    const accounts = getProviderAccounts(provider);
    const lowerQuery = query.toLowerCase();
    const exactIdMatch = accounts.find((a) => a.id === query);
    if (exactIdMatch)
        return exactIdMatch;
    const emailMatches = accounts.filter((a) => a.email?.toLowerCase() === lowerQuery);
    if (emailMatches.length === 1)
        return emailMatches[0];
    if (emailMatches.length > 1)
        return null;
    const nicknameMatches = accounts.filter((a) => a.nickname?.toLowerCase() === lowerQuery);
    if (nicknameMatches.length === 1)
        return nicknameMatches[0];
    if (nicknameMatches.length > 1)
        return null;
    // Partial match on nickname or email prefix
    const partialMatches = accounts.filter((a) => a.nickname?.toLowerCase().startsWith(lowerQuery) ||
        a.email?.toLowerCase().startsWith(lowerQuery));
    return partialMatches.length === 1 ? partialMatches[0] : null;
}
exports.findAccountByQuery = findAccountByQuery;
/**
 * Get non-paused accounts for a provider
 */
function getActiveAccounts(provider) {
    return getProviderAccounts(provider).filter((a) => !a.paused);
}
exports.getActiveAccounts = getActiveAccounts;
/**
 * Check if an account is paused
 */
function isAccountPaused(provider, accountId) {
    const accounts = getProviderAccounts(provider);
    const account = accounts.find((a) => a.id === accountId);
    return account?.paused ?? false;
}
exports.isAccountPaused = isAccountPaused;
/**
 * Get summary of all accounts across providers
 */
function getAllAccountsSummary() {
    const providers = [...profile_detector_1.CLIPROXY_PROFILES];
    const summary = {};
    for (const provider of providers) {
        summary[provider] = getProviderAccounts(provider);
    }
    return summary;
}
exports.getAllAccountsSummary = getAllAccountsSummary;
//# sourceMappingURL=query.js.map