// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// engine/registry/loader.ts — Loads and merges framework registry entries
// Base entries come from src/config/framework-registry/*.json
// Custom entries come from data/global/registry-custom.json (user-added)
import { readFile, readdir } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readJson } from '../../storage/base-store.js';
const REGISTRY_DIR = join(dirname(fileURLToPath(import.meta.url)), '../../config/framework-registry');
const CUSTOM_REGISTRY_PATH = 'data/global/registry-custom.json';
/**
 * Load a single framework JSON file from the base registry directory.
 * Returns null if the file is missing or malformed.
 */
async function loadFrameworkFile(fileName) {
    try {
        const raw = await readFile(join(REGISTRY_DIR, fileName), 'utf-8');
        const parsed = JSON.parse(raw);
        return {
            framework: parsed.framework,
            displayName: parsed.displayName,
            frameworkVersion: parsed.frameworkVersion,
            lastUpdated: parsed.lastUpdated,
            ecosystem: parsed.ecosystem,
            customEntry: false,
            correctPatterns: Array.isArray(parsed.correctPatterns)
                ? parsed.correctPatterns
                : [],
            deprecatedPatterns: Array.isArray(parsed.deprecatedPatterns)
                ? parsed.deprecatedPatterns
                : [],
            commonMistakes: Array.isArray(parsed.commonMistakes)
                ? parsed.commonMistakes
                : [],
        };
    }
    catch {
        return null;
    }
}
/**
 * Load all base framework entries from src/config/framework-registry/.
 * Skips index.json and any file that fails to parse.
 */
export async function loadBaseRegistry() {
    let files;
    try {
        files = await readdir(REGISTRY_DIR);
    }
    catch {
        return [];
    }
    const jsonFiles = files.filter((f) => f.endsWith('.json') && f !== 'index.json');
    const results = await Promise.all(jsonFiles.map(loadFrameworkFile));
    return results.filter((e) => e !== null);
}
/**
 * Load user-defined custom registry entries from data/global/registry-custom.json.
 * Returns an empty array when the file does not exist.
 */
export async function loadCustomRegistry() {
    const store = await readJson(CUSTOM_REGISTRY_PATH, { entries: [] });
    return store.entries.map((e) => ({ ...e, customEntry: true }));
}
/**
 * Load and merge the full registry: base + custom.
 * Custom entries override base entries with the same framework ID.
 * The merged map preserves custom entries for duplicates.
 */
export async function loadRegistry() {
    const [base, custom] = await Promise.all([loadBaseRegistry(), loadCustomRegistry()]);
    const map = new Map();
    for (const entry of base) {
        map.set(entry.framework, entry);
    }
    // Custom overrides base for same framework ID
    for (const entry of custom) {
        map.set(entry.framework, { ...entry, customEntry: true });
    }
    return map;
}
//# sourceMappingURL=loader.js.map