/**
 * Lightweight i18n: flat key → translated string with `{name}` interpolation.
 *
 * Resolution order for the active locale at a given call site:
 *   1. explicit `locale` argument to `t()`
 *   2. per-bot `lang` config (resolved via `botLocale()`)
 *   3. process default — set by the entrypoint from `~/.botmux/config.json`
 *      (`setDefaultLocale(...)`), falling back to `'zh'` for backward compat.
 *
 * The i18n module itself stays pure — it does not read the filesystem. The
 * CLI and daemon entrypoints load the global config and call
 * `setDefaultLocale(...)` before any user-facing string is emitted.
 */
import { messages as zhMessages } from './zh.js';
import { messages as enMessages } from './en.js';
import { isLocale } from './types.js';
export { isLocale, SUPPORTED_LOCALES } from './types.js';
const dictionaries = {
    zh: zhMessages,
    en: enMessages,
};
let defaultLocale = 'zh';
export function getDefaultLocale() {
    return defaultLocale;
}
export function setDefaultLocale(loc) {
    defaultLocale = loc;
}
/** Resolve the locale for a given bot's config (used by per-bot code paths). */
export function botLocale(botCfg) {
    if (botCfg && isLocale(botCfg.lang))
        return botCfg.lang;
    return defaultLocale;
}
let botLookup;
/**
 * Register a bot-config lookup so `localeForBot()` can resolve a per-bot
 * locale without creating an import cycle between `i18n` and `bot-registry`.
 * Called once by `bot-registry.ts` at module load.
 */
export function setBotLookup(lookup) {
    botLookup = lookup;
}
/**
 * Resolve the locale for a bot by its larkAppId. Falls back to the process
 * default when the bot is not registered (e.g. CLI tools without a daemon).
 */
export function localeForBot(larkAppId) {
    if (!larkAppId || !botLookup)
        return defaultLocale;
    try {
        return botLocale(botLookup(larkAppId)?.config);
    }
    catch {
        return defaultLocale;
    }
}
function interpolate(template, params) {
    return template.replace(/\{(\w+)\}/g, (_, k) => {
        const v = params[k];
        return v === undefined || v === null ? `{${k}}` : String(v);
    });
}
/**
 * Translate a key. Falls back to the Chinese dictionary, then to the key
 * itself if neither dictionary has the entry (so missing keys are loud
 * rather than silently producing empty strings).
 */
export function t(key, params, locale) {
    const loc = locale ?? defaultLocale;
    const tpl = dictionaries[loc]?.[key] ?? dictionaries.zh[key] ?? key;
    return params ? interpolate(tpl, params) : tpl;
}
//# sourceMappingURL=index.js.map