import { modelsAreEqual } from "@earendil-works/pi-ai/compat";
import chalk from "chalk";
import { minimatch } from "minimatch";
import { isValidThinkingLevel } from "../cli/args.js";
import { parseModelPattern } from "./model-resolver-patterns.js";
function hasGlobCharacters(pattern) {
    return pattern.includes("*") || pattern.includes("?") || pattern.includes("[");
}
function parseGlobThinkingLevel(pattern) {
    const colonIdx = pattern.lastIndexOf(":");
    if (colonIdx === -1) {
        return { globPattern: pattern };
    }
    const suffix = pattern.substring(colonIdx + 1);
    if (!isValidThinkingLevel(suffix)) {
        return { globPattern: pattern };
    }
    return { globPattern: pattern.substring(0, colonIdx), thinkingLevel: suffix };
}
/**
 * Resolve model patterns to actual Model objects with optional thinking levels
 * Format: "pattern:level" where :level is optional
 * For each pattern, finds all matching models and picks the best version:
 * 1. Prefer alias (e.g., claude-sonnet-4-5) over dated versions (claude-sonnet-4-5-20250929)
 * 2. If no alias, pick the latest dated version
 *
 * Supports models with colons in their IDs (e.g., OpenRouter's model:exacto).
 * The algorithm tries to match the full pattern first, then progressively
 * strips colon-suffixes to find a match.
 */
export async function resolveModelScopeWithDiagnostics(patterns, modelRegistry) {
    const availableModels = await modelRegistry.getAvailable();
    const scopedModels = [];
    const diagnostics = [];
    for (const pattern of patterns) {
        if (hasGlobCharacters(pattern)) {
            const { globPattern, thinkingLevel } = parseGlobThinkingLevel(pattern);
            const matchingModels = availableModels.filter((m) => {
                const fullId = `${m.provider}/${m.id}`;
                return minimatch(fullId, globPattern, { nocase: true }) || minimatch(m.id, globPattern, { nocase: true });
            });
            if (matchingModels.length === 0) {
                diagnostics.push({ type: "warning", message: `No models match pattern "${pattern}"` });
                continue;
            }
            for (const model of matchingModels) {
                if (!scopedModels.find((sm) => modelsAreEqual(sm.model, model))) {
                    scopedModels.push({ model, thinkingLevel });
                }
            }
            continue;
        }
        const { model, thinkingLevel, warning } = parseModelPattern(pattern, availableModels);
        if (warning) {
            diagnostics.push({ type: "warning", message: warning });
        }
        if (!model) {
            diagnostics.push({ type: "warning", message: `No models match pattern "${pattern}"` });
            continue;
        }
        if (!scopedModels.find((sm) => modelsAreEqual(sm.model, model))) {
            scopedModels.push({ model, thinkingLevel });
        }
    }
    return { scopedModels, diagnostics };
}
export async function resolveModelScope(patterns, modelRegistry) {
    const { scopedModels, diagnostics } = await resolveModelScopeWithDiagnostics(patterns, modelRegistry);
    for (const diagnostic of diagnostics) {
        console.warn(chalk.yellow(`Warning: ${diagnostic.message}`));
    }
    return scopedModels;
}
//# sourceMappingURL=model-resolver-scope.js.map