import { logger } from '../utils/logger.js';
import { summarizeError } from '../utils/error-display.js';
import { fetchCatalog, isCatalogCacheStale, loadCatalogCache, notifyCatalogChanges, saveCatalogCache, } from './model-catalog.js';
/**
 * Load the catalog from disk cache on startup. If the cache is absent or
 * stale, schedules a background refresh without blocking the caller.
 *
 * Extracted from ProviderRegistry.initCatalog() to isolate catalog
 * initialization logic from provider management concerns.
 */
export function initProviderCatalog(ctx) {
    const cached = loadCatalogCache(ctx.getCatalogCachePaths().cachePath);
    if (cached) {
        ctx.updateCatalogState(cached.models, cached.fetchedAt);
    }
    if (!cached || isCatalogCacheStale(cached)) {
        void ctx.refreshCatalog().catch((err) => {
            logger.warn('[model-catalog] Background refresh failed', { error: summarizeError(err) });
        });
    }
}
/**
 * Fetch an updated catalog from the remote source, persist it to the local
 * cache, apply it to the registry state, and emit change notifications.
 *
 * Extracted from ProviderRegistry.refreshCatalog() to isolate catalog
 * refresh logic from provider management concerns.
 */
export async function refreshProviderCatalog(ctx) {
    const previous = [...ctx.getCatalogModels()];
    const models = await fetchCatalog();
    if (models.length === 0) {
        logger.warn('[model-catalog] Refresh returned 0 models — keeping existing catalog');
        return;
    }
    const { cachePath, tmpPath } = ctx.getCatalogCachePaths();
    saveCatalogCache(models, cachePath, tmpPath);
    ctx.updateCatalogState(models);
    const favorites = await ctx.favoritesStore.load();
    notifyCatalogChanges(previous, [...ctx.getCatalogModels()], favorites, ctx.benchmarkStore.getTopBenchmarkModelIds(10));
    logger.debug('[model-catalog] Catalog updated', { count: models.length });
}
