// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// engine/skill-registry/builtin-catalog-adapter.ts — SPEC-307: Built-in skill catalog search
// Searches src/config/skill-catalog.json for built-in skills without network calls.
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
let catalogCache = null;
/** Load the built-in skill catalog from disk (cached after first read). */
function loadCatalog() {
    if (catalogCache !== null) {
        return catalogCache;
    }
    // Try compiled path first (dist/config/), then source path (src/config/)
    const compiledPath = join(__dirname, '../../config/skill-catalog.json');
    const sourcePath = join(__dirname, '../../../src/config/skill-catalog.json');
    for (const configPath of [compiledPath, sourcePath]) {
        let raw;
        try {
            raw = readFileSync(configPath, 'utf-8');
        }
        catch {
            continue;
        }
        try {
            const catalog = JSON.parse(raw);
            catalogCache =
                Array.isArray(catalog.skills) && catalog.skills.length > 0 ? catalog.skills : [];
            return catalogCache;
        }
        catch {
            console.warn('[skill_search] skill-catalog.json is corrupted, skipping built-in catalog');
            return [];
        }
    }
    return [];
}
/** Compute a simple relevance score for a built-in skill against a query. */
function scoreBuiltIn(skill, query) {
    const q = query.toLowerCase();
    const nameLower = skill.name.toLowerCase();
    const descLower = skill.description.toLowerCase();
    const categoryLower = skill.category.toLowerCase();
    const tagMatch = skill.tags.some((t) => t.toLowerCase().includes(q));
    let score = 0;
    if (nameLower === q) {
        score += 0.45;
    }
    else if (nameLower.includes(q)) {
        score += 0.3;
    }
    if (descLower.includes(q)) {
        score += 0.15;
    }
    if (categoryLower.includes(q)) {
        score += 0.1;
    }
    if (tagMatch) {
        score += 0.1;
    }
    // Small boost for built-in skills to surface them alongside remote results
    score += 0.02;
    return Math.min(score, 1);
}
/**
 * Search built-in skills from the skill catalog.
 * Returns all matching skills regardless of query for built-in catalog browsing.
 */
export function searchBuiltInSkills(query) {
    const catalog = loadCatalog();
    const q = query.toLowerCase();
    const results = [];
    for (const skill of catalog) {
        const score = scoreBuiltIn(skill, q);
        // Include if any keyword matches (score > 0.02 means something matched beyond the boost)
        if (score > 0.03) {
            results.push({
                name: skill.name,
                description: skill.description,
                source: 'builtin',
                installCommand: skill.installCommand,
                tags: skill.tags,
                relevanceScore: score,
            });
        }
    }
    return results;
}
/** Get a single built-in skill entry by exact name. Returns undefined if not found. */
export function getBuiltInSkillEntry(name) {
    return loadCatalog().find((s) => s.name === name);
}
/** Clear the catalog cache (for testing). */
export function clearBuiltInCatalogCache() {
    catalogCache = null;
}
//# sourceMappingURL=builtin-catalog-adapter.js.map