// ---------------------------------------------------------------------------
// 0711 — model-resolver
//
// Single chokepoint that turns an alias / canonical id / env override into a
// fully-resolved record with provenance. Replaces the scattered
// `ANTHROPIC_NORMALIZE` lookups in llm.ts so that:
//   - `aiMeta.model` now records the concrete ID rather than the alias,
//     making "regenerated on Opus 4.6 vs 4.7" diffable.
//   - Pricing + display name come from a SINGLE catalog file; fixing one
//     stale ref no longer means hunting through three modules.
//   - ENV override (`VSKILL_DEFAULT_MODEL_ANTHROPIC`) lets early-access users
//     route plain `opus`/`sonnet`/`haiku` aliases to a not-yet-shipped model
//     without code changes.
//
// Pure function. Server + browser safe (no node:* imports). Server-side
// runtime catalog refresh lives in `catalog-fetcher.ts` (separate file) and
// is OPTIONAL — when offline, the dated snapshot is the source of truth.
// ---------------------------------------------------------------------------
import { ANTHROPIC_CATALOG_SNAPSHOT, findAnthropicModel, } from "./anthropic-catalog.js";
const PLAIN_ALIASES = new Set(["opus", "sonnet", "haiku", "best"]);
function passthrough(requestedAlias, catalog) {
    return {
        requestedAlias,
        resolvedId: requestedAlias,
        displayName: "",
        pricing: null,
        source: "passthrough",
        snapshotDate: catalog.snapshotDate,
        status: null,
        sunsetDate: null,
    };
}
function fromCatalog(requestedAlias, entry, source, catalog) {
    return {
        requestedAlias,
        resolvedId: entry.id,
        displayName: entry.displayName,
        pricing: entry.pricing,
        source,
        snapshotDate: catalog.snapshotDate,
        status: entry.status,
        sunsetDate: entry.sunsetDate,
    };
}
/**
 * Resolve an Anthropic alias / canonical ID into a provenance-rich record.
 *
 * Precedence:
 *   1. ENV override `VSKILL_DEFAULT_MODEL_ANTHROPIC` — only when the input is
 *      a plain alias (`opus|sonnet|haiku|best`) so explicit canonical IDs
 *      always win over a tuned default.
 *   2. Catalog lookup — alias OR canonical ID, case-insensitive.
 *   3. Passthrough — unknown future ID; keep the input as `resolvedId`,
 *      record `source: "passthrough"` so the UI can warn about pricing
 *      and the test suite can flag missing entries.
 */
export function resolveAnthropicModel(input, opts = {}) {
    const catalog = opts.catalog ?? ANTHROPIC_CATALOG_SNAPSHOT;
    const env = opts.env ?? process.env;
    const requestedAlias = input ?? "";
    const trimmed = requestedAlias.trim();
    if (!trimmed)
        return passthrough(requestedAlias, catalog);
    // 1. ENV override only kicks in for plain aliases — explicit canonical IDs
    //    are honoured exactly as the caller wrote them.
    const isPlainAlias = PLAIN_ALIASES.has(trimmed.toLowerCase());
    const overrideRaw = env.VSKILL_DEFAULT_MODEL_ANTHROPIC;
    const override = typeof overrideRaw === "string" ? overrideRaw.trim() : "";
    if (isPlainAlias && override) {
        const overrideEntry = findAnthropicModel(override, catalog);
        if (overrideEntry) {
            return fromCatalog(requestedAlias, overrideEntry, "env-override", catalog);
        }
        // Override is a future ID we do not know yet — preserve provenance and
        // signal that the snapshot is stale.
        return {
            requestedAlias,
            resolvedId: override,
            displayName: "",
            pricing: null,
            source: "env-override",
            snapshotDate: catalog.snapshotDate,
            status: null,
            sunsetDate: null,
        };
    }
    // 2. Catalog lookup.
    const entry = findAnthropicModel(trimmed, catalog);
    if (entry)
        return fromCatalog(requestedAlias, entry, "snapshot", catalog);
    // 3. Passthrough.
    return passthrough(trimmed, catalog);
}
//# sourceMappingURL=model-resolver.js.map