import { readFileSync, readdirSync, existsSync } from "node:fs";
import { join, basename, extname } from "node:path";
import { logger } from "../utils/logger.js";
export class PatternLibrary {
    patterns = new Map();
    constructor(patternsDir) {
        this.load(patternsDir);
    }
    load(dir) {
        if (!existsSync(dir)) {
            logger.debug(`Patterns directory not found: ${dir}`);
            return;
        }
        try {
            const files = readdirSync(dir).filter((f) => extname(f) === ".json");
            for (const file of files) {
                try {
                    const raw = readFileSync(join(dir, file), "utf-8");
                    const pattern = JSON.parse(raw);
                    const key = basename(file, ".json");
                    this.patterns.set(key, pattern);
                }
                catch (e) {
                    logger.warn(`Failed to load pattern ${file}: ${e}`);
                }
            }
            logger.debug(`Loaded ${this.patterns.size} mod patterns`);
        }
        catch (e) {
            logger.warn(`Failed to read patterns directory: ${e}`);
        }
    }
    get(name) {
        return this.patterns.get(name);
    }
    list() {
        return [...this.patterns.keys()];
    }
    getAll() {
        return [...this.patterns.values()];
    }
    /** Get a summary of all patterns for tool descriptions */
    getSummary() {
        if (this.patterns.size === 0)
            return "No patterns loaded.";
        const lines = [];
        for (const [key, pattern] of this.patterns) {
            lines.push(`- **${key}**: ${pattern.description}`);
        }
        return lines.join("\n");
    }
}
//# sourceMappingURL=loader.js.map