import { getAllExampleMetadata, formatExamplesByIds, VALID_TAGS, } from "./examples.js";
// Create a Set for efficient tag validation
const VALID_TAGS_SET = new Set(VALID_TAGS);
/**
 * Select examples based on provided tags.
 * Returns the most relevant examples ordered by relevance.
 */
export function selectExamplesByTags(tags, options = {}) {
    const { maxExamples = 2, includeNegative = false, maxTokenBudget = 2000, } = options;
    // Get all example metadata
    const EXAMPLE_METADATA = getAllExampleMetadata();
    // If no tags provided, return one simple baseline example
    if (!tags || tags.length === 0) {
        const baseline = EXAMPLE_METADATA.find((e) => e.id === "getUsersByStatusApi");
        return baseline ? [baseline] : [];
    }
    // Normalize tags
    const tagSet = new Set(tags.map((t) => t.toLowerCase().trim()));
    // Filter out invalid tags
    const validTagSet = new Set(Array.from(tagSet).filter((t) => VALID_TAGS_SET.has(t)));
    // If no valid tags, return baseline
    if (validTagSet.size === 0) {
        const baseline = EXAMPLE_METADATA.find((e) => e.id === "getUsersByStatusApi");
        return baseline ? [baseline] : [];
    }
    // Add some tags by default
    validTagSet.add("global-context");
    validTagSet.add("error-scope-violation");
    // Filter examples based on category preference
    const candidateExamples = includeNegative
        ? EXAMPLE_METADATA
        : EXAMPLE_METADATA.filter((e) => e.category === "positive");
    // Score each example by tag overlap
    const scored = candidateExamples.map((metadata) => {
        const exampleTagSet = new Set(metadata.tags.map((t) => t.toLowerCase()));
        const matchedTags = Array.from(validTagSet).filter((t) => exampleTagSet.has(t));
        // Base score: number of matching tags
        let score = matchedTags.length;
        // Boost for complexity match with nesting tags
        if (validTagSet.has("nested-control-flow")) {
            if (metadata.complexity === "complex") {
                score += 2;
            }
        }
        // Boost for negative examples when explicitly requested
        if (metadata.category === "negative") {
            score += 3;
        }
        // Boost for scope-related examples (high error rate)
        if (validTagSet.has("scope-access") ||
            validTagSet.has("error-scope-violation")) {
            if (metadata.tags.includes("scope-access") ||
                metadata.tags.includes("error-scope-violation")) {
                score += 2;
            }
        }
        return { metadata, score, matchedTags };
    });
    // Sort by score (descending)
    scored.sort((a, b) => b.score - a.score);
    // Select top examples within token budget
    const selected = [];
    let currentTokens = 0;
    for (const { metadata, score } of scored) {
        // Only include if it has at least one matching tag
        if (score === 0)
            continue;
        // Check token budget
        if (currentTokens + metadata.estimatedTokens > maxTokenBudget) {
            break;
        }
        selected.push(metadata);
        currentTokens += metadata.estimatedTokens;
        // Stop if we've reached max examples
        if (selected.length >= maxExamples) {
            break;
        }
    }
    // If no examples matched, return one simple baseline
    if (selected.length === 0) {
        const baseline = EXAMPLE_METADATA.find((e) => e.id === "getUsersByStatusApi");
        if (baseline) {
            selected.push(baseline);
        }
    }
    return selected;
}
/**
 * Format selected examples for inclusion in the system prompt.
 */
export function formatSelectedExamples(selectedMetadata) {
    if (selectedMetadata.length === 0) {
        return "";
    }
    const ids = selectedMetadata.map((m) => m.id);
    const extractedExamples = formatExamplesByIds(ids);
    if (!extractedExamples) {
        return "";
    }
    // Add a header explaining these are selected examples
    const header = `The following examples demonstrate relevant patterns for this API task:\n\n`;
    return header + extractedExamples;
}
//# sourceMappingURL=example-selector.js.map