// engine/spec-templates/query.ts — Catalog querying: list and find templates.
import { BUILT_IN_TEMPLATES } from './catalog.js';
import { loadCustomTemplates, mergeTemplates } from './custom-loader.js';
/**
 * Converts a full SpecTemplateEntry to a summary TemplateListItem.
 */
function toListItem(template) {
    return {
        id: template.id,
        name: template.name,
        category: template.category,
        subcategory: template.subcategory,
        description: template.description,
        tags: template.tags,
        complexityScore: template.complexityScore,
        criteriaCount: template.criteria.length,
        variableCount: template.variables.length,
        requiredVariableCount: template.variables.filter((v) => v.required).length,
    };
}
/**
 * Lists templates from the catalog, optionally filtered by category, search, and complexity.
 * Includes custom user templates from data/global/spec-templates/custom/.
 */
export async function listTemplates(category, search, complexityScore, customDir) {
    const customResult = await loadCustomTemplates(customDir);
    const allTemplates = mergeTemplates(BUILT_IN_TEMPLATES, customResult.templates);
    let templates = [...allTemplates];
    if (category !== undefined) {
        templates = templates.filter((t) => t.category === category);
    }
    if (complexityScore !== undefined) {
        templates = templates.filter((t) => t.complexityScore === complexityScore);
    }
    if (search !== undefined && search.trim().length > 0) {
        const term = search.trim().toLowerCase();
        templates = templates.filter((t) => t.name.toLowerCase().includes(term) ||
            t.description.toLowerCase().includes(term) ||
            t.tags.some((tag) => tag.toLowerCase().includes(term)));
    }
    const filterLabel = [
        category !== undefined ? `category=${category}` : '',
        complexityScore !== undefined ? `complexity=${complexityScore}` : '',
        search !== undefined && search.trim().length > 0 ? `search="${search.trim()}"` : '',
    ]
        .filter(Boolean)
        .join(', ');
    return {
        result: {
            templates: templates.map(toListItem),
            total: templates.length,
            filteredBy: filterLabel.length > 0 ? filterLabel : undefined,
        },
        warnings: customResult.warnings,
    };
}
/**
 * Returns a single template by ID (built-in only, sync).
 */
export function findTemplate(templateId) {
    return BUILT_IN_TEMPLATES.find((t) => t.id === templateId);
}
/**
 * Returns a single template by ID, searching built-in and custom templates.
 */
export async function findTemplateWithCustom(templateId, customDir) {
    const customResult = await loadCustomTemplates(customDir);
    const allTemplates = mergeTemplates(BUILT_IN_TEMPLATES, customResult.templates);
    return allTemplates.find((t) => t.id === templateId);
}
/**
 * Returns all available categories that have at least one template.
 */
export function getAvailableCategories() {
    const categories = new Set(BUILT_IN_TEMPLATES.map((t) => t.category));
    return [...categories];
}
//# sourceMappingURL=query.js.map