// engine/coach/i18n.ts — i18n helper for coach rule packs (SPEC-749)
// Reads from src/i18n/coach/{locale}.json with en fallback.
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
// ---------------------------------------------------------------------------
// Internal catalog cache
// ---------------------------------------------------------------------------
const catalogCache = new Map();
function catalogDir() {
    const thisFile = fileURLToPath(import.meta.url);
    const thisDir = dirname(thisFile);
    // Works from both src/ (dev) and dist/ (compiled) by looking up two levels
    const srcPath = join(thisDir, '..', '..', 'i18n', 'coach');
    const distFallback = join(thisDir, '..', '..', '..', 'src', 'i18n', 'coach');
    try {
        readFileSync(join(srcPath, 'en.json'), 'utf-8');
        return srcPath;
    }
    catch {
        return distFallback;
    }
}
function loadCatalog(locale) {
    const cached = catalogCache.get(locale);
    if (cached !== undefined) {
        return cached;
    }
    try {
        const raw = readFileSync(join(catalogDir(), `${locale}.json`), 'utf-8');
        const parsed = JSON.parse(raw);
        catalogCache.set(locale, parsed);
        return parsed;
    }
    catch {
        const empty = {};
        catalogCache.set(locale, empty);
        return empty;
    }
}
// ---------------------------------------------------------------------------
// Public t() helper
// ---------------------------------------------------------------------------
/**
 * Translate a coach i18n key with optional param interpolation.
 * Falls back to 'en' when key is absent in the requested locale.
 * Emits a telemetry-style console warning for missing keys (non-en).
 */
export function t(key, params, locale = 'en') {
    const catalog = loadCatalog(locale);
    let message = catalog[key];
    if (message === undefined && locale !== 'en') {
        // Fallback to English
        const enCatalog = loadCatalog('en');
        message = enCatalog[key];
        if (message !== undefined) {
            // Telemetry event — emit as warning for observability
            console.warn(`[coach.i18n.missing_key] locale=${locale} key=${key}`);
        }
    }
    if (message === undefined) {
        return key;
    }
    // Interpolate {param} placeholders
    if (params !== undefined) {
        for (const [param, value] of Object.entries(params)) {
            message = message.replaceAll(`{${param}}`, String(value));
        }
    }
    return message;
}
/**
 * Clear the catalog cache — useful in tests.
 */
export function clearCatalogCache() {
    catalogCache.clear();
}
//# sourceMappingURL=i18n.js.map