function toModelSnapshot(model, providerRegistry) {
    const pricing = providerRegistry.getPricingForModel(model.id, model.provider);
    const cost = pricing
        ? { input: pricing.prompt, output: pricing.completion }
        : providerRegistry.getCostFromCatalog(model.id);
    return {
        id: model.id,
        registryKey: model.registryKey,
        displayName: model.displayName,
        selectable: model.selectable,
        contextWindow: model.contextWindow,
        ...(model.tier ? { tier: model.tier } : {}),
        ...(cost
            ? {
                pricing: {
                    inputPerMillionTokens: cost.input,
                    outputPerMillionTokens: cost.output,
                    currency: 'USD',
                },
            }
            : {}),
    };
}
async function buildSnapshotForProvider(providerRegistry, providerId) {
    let provider;
    try {
        provider = providerRegistry.getRegistered(providerId);
    }
    catch {
        return null;
    }
    const runtime = provider.describeRuntime
        ? await providerRegistry.describeRuntime(providerId)
        : {
            auth: { mode: 'none', configured: false, detail: 'Provider does not expose runtime metadata.' },
            models: { models: provider.models },
            usage: { streaming: true, toolCalling: true, parallelTools: false },
        };
    const resolvedRuntime = runtime ?? {
        auth: { mode: 'none', configured: false, detail: 'Provider does not expose runtime metadata.' },
        models: { models: provider.models },
        usage: { streaming: true, toolCalling: true, parallelTools: false },
    };
    const currentModel = providerRegistry.getCurrentModel();
    const models = providerRegistry
        .listModels()
        .filter((model) => model.provider === providerId)
        .map((model) => toModelSnapshot(model, providerRegistry));
    return {
        providerId,
        active: currentModel.provider === providerId,
        modelCount: models.length,
        runtime: resolvedRuntime,
        models,
    };
}
export async function listProviderRuntimeSnapshots(providerRegistry) {
    const snapshots = await Promise.all(providerRegistry.listProviders().map((provider) => buildSnapshotForProvider(providerRegistry, provider.name)));
    return snapshots.filter((snapshot) => snapshot != null);
}
export async function getProviderRuntimeSnapshot(providerRegistry, providerId) {
    return buildSnapshotForProvider(providerRegistry, providerId);
}
export async function getProviderUsageSnapshot(providerRegistry, providerId) {
    const snapshot = await buildSnapshotForProvider(providerRegistry, providerId);
    if (!snapshot)
        return null;
    const currentModel = providerRegistry.getCurrentModel();
    const usage = snapshot.runtime.usage ?? {
        streaming: true,
        toolCalling: true,
        parallelTools: false,
    };
    return {
        providerId,
        active: snapshot.active,
        ...(currentModel.provider === providerId ? { currentModelRegistryKey: currentModel.registryKey } : {}),
        pricingSource: snapshot.models.some((model) => model.pricing) ? 'catalog' : (usage.cost?.source ?? 'none'),
        models: snapshot.models,
        usage,
    };
}
