import { createContext, useContext, type ReactNode } from "react";

export type Language = "zh-CN" | "en";

type TranslationParams = Record<string, string | number | null | undefined>;
type TranslationEntry = string | ((params: TranslationParams) => string);
type TranslationTable = Record<string, TranslationEntry>;
type Translator = (key: string, params?: TranslationParams) => string;

export const LANGUAGE_STORAGE_KEY = "minibot_web_language";

const messages: Record<Language, TranslationTable> = {
  "zh-CN": {
    "common.on": "开启",
    "common.off": "关闭",
    "common.enabled": "启用",
    "common.disabled": "禁用",
    "common.inherit": "继承",
    "common.add": "添加",
    "common.remove": "移除",
    "common.cancel": "取消",
    "common.create": "创建",
    "common.delete": "删除",
    "common.refresh": "刷新",
    "common.search": "搜索",
    "common.none": "无",
    "common.notSet": "未设置",
    "common.unknown": "未知",
    "common.current": "当前",
    "common.selectAll": "全选",
    "common.clearSelection": "清空选择",
    "common.save": "保存",
    "common.loading": "加载中",
    "common.available": "可用",
    "common.attached": "已附加",
    "common.default": "默认",
    "app.title.chat": "Minibot • 聊天",
    "app.title.config": "Minibot • 配置",
    "app.brand": "Minibot",
    "app.tab.chat": "聊天",
    "app.tab.config": "配置",
    "app.bot.new": "新建 Bot",
    "app.bot.delete": "删除",
    "app.bot.create.title": "创建 Bot",
    "app.bot.create.description": "为你的 Bot 取一个友好的名字（可选）。",
    "app.bot.create.label": "Bot 名称",
    "app.bot.create.placeholder": "例如：Mini QA",
    "app.bot.create.confirm": "创建",
    "app.bot.delete.title": "删除 Bot",
    "app.bot.delete.description":
      "删除 {name} 以及它的全部会话？此操作无法撤销。",
    "language.button.title": "语言",
    "language.button.aria": "当前语言：{current}。打开语言菜单。",
    "language.menu.label": "界面语言",
    "language.option.zh": "中文",
    "language.option.zh.description": "界面文案显示为简体中文",
    "language.option.en": "English",
    "language.option.en.description": "界面文案显示为英文",
    "theme.mode.light": "浅色",
    "theme.mode.dark": "深色",
    "theme.mode.system": "跟随系统",
    "theme.button.aria": "当前主题：{current}。点击切换到 {next}。",
    "chat.tool.badge": "工具",
    "chat.tool.arguments": "参数",
    "chat.tool.noArguments": "没有参数。",
    "chat.tool.result": "结果",
    "chat.tool.waiting": "等待工具结果...",
    "chat.tool.noResult": "没有结果。",
    "chat.tool.status.running": "运行中",
    "chat.tool.status.done": "完成",
    "chat.tool.status.error": "错误",
    "chat.status.copiedReply": "已复制回复。",
    "chat.status.deletedTurn": "已删除该轮消息。",
    "chat.status.regeneratedReply": "已重新生成最新回复。",
    "chat.status.chatModelUpdated": "聊天模型已更新。",
    "chat.status.usingFallbackModel": "已切换为使用 Bot 的后备模型。",
    "chat.fallbackDisabledReason": "此 Bot 已被禁用。",
    "chat.sessions.title": "会话",
    "chat.sessions.new": "新建",
    "chat.sessions.refresh": "刷新",
    "chat.sessions.noPreview": "（无预览）",
    "chat.sessions.deleteTitle": "删除会话",
    "chat.sessions.deleteShort": "删",
    "chat.sessions.empty": "暂无会话。等这个 Bot 可聊天后，再开始一个新会话。",
    "chat.header.label": "聊天",
    "chat.header.session": "会话：",
    "chat.header.none": "（无）",
    "chat.header.disabled": "已禁用",
    "chat.model.label": "聊天模型",
    "chat.model.fallbackOption": "后备 Bot 模型",
    "chat.model.stop": "停止",
    "chat.model.usingSelected": "当前使用 {provider} / {label}",
    "chat.model.usingFallback":
      ({ model }) => (model ? `当前使用后备模型：${model}` : "当前使用后备模型"),
    "chat.reasoning.title": "深度思考",
    "chat.message.copy": "复制",
    "chat.message.regenerate": "重新生成",
    "chat.message.delete": "删除",
    "chat.message.tokens": "Tokens {count}",
    "chat.empty.blocked": "请先解决上方的 Bot 或模型问题，再发送消息。",
    "chat.empty.ready": "从发送一条消息开始。",
    "chat.input.blocked": "此 Bot 当前不可聊天。",
    "chat.input.ready": "向 Minibot 发送消息...",
    "chat.input.send": "发送",
    "chat.input.hint": "Enter 发送 • Shift+Enter 换行",
    "chat.deleteSession.title": "删除会话",
    "chat.deleteSession.description":
      "删除 {sessionId} 并从这个 Bot 中移除其消息？此操作无法撤销。",
    "config.status.botSaved": "Bot 配置已保存。",
    "config.status.providerUpdated": "Provider 已更新。",
    "config.status.providerAdded": "Provider 已添加。",
    "config.status.importedModels":
      ({ count }) => `已导入 ${count} 个模型。`,
    "config.status.deletedModels":
      ({ count }) => `已删除 ${count} 个模型。`,
    "config.summary.providers": "Providers",
    "config.summary.models": "Models",
    "config.summary.bots": "Bots",
    "config.summary.subagents": "子代理",
    "config.header.title": "控制面板",
    "config.header.subtitle": "在一个页面里统一管理 providers、models、bots 和子代理关系。",
    "config.tab.bots": "Bots",
    "config.tab.providers": "Providers",
    "config.loading": "正在加载控制面板...",
    "config.section.allBots.title": "全部 Bots",
    "config.section.allBots.subtitle": "点击某个 Bot，将其设为当前编辑目标。",
    "config.bot.tag.default": "默认",
    "config.bot.tag.subagentReady": "可作子代理",
    "config.bot.chatUnavailable": "聊天不可用",
    "config.section.botDetail.title":
      ({ name }) => (name ? `正在编辑 ${name}` : "Bot 详情"),
    "config.section.botDetail.subtitle": "此面板控制当前选中的 Bot。",
    "config.bot.save": "保存 Bot",
    "config.bot.saving": "保存中...",
    "config.bot.name": "Bot 名称",
    "config.bot.name.placeholder": "我的 Bot",
    "config.bot.enabled": "启用 Bot",
    "config.bot.enabled.subtitle": "被禁用的 Bot 不能用于聊天，也不能作为子代理。",
    "config.bot.chatModel": "聊天模型",
    "config.bot.chatModel.fallback": "使用高级后备模型",
    "config.bot.streaming": "流式输出",
    "config.bot.streaming.subtitle": "在聊天中启用逐 token 输出。",
    "config.section.subagentIdentity.title": "子代理身份",
    "config.section.subagentIdentity.subtitle": "控制其他 Bot 是否能把当前 Bot 挂接为子代理。",
    "config.bot.allowSubagent": "允许作为子代理",
    "config.bot.allowSubagent.subtitle": "启用后，这个 Bot 会出现在其他 Bot 的子代理选择器里。",
    "config.bot.subagentDisplayName": "子代理显示名",
    "config.bot.subagentDescription": "子代理描述",
    "config.bot.subagentDescription.placeholder": "描述在什么情况下应把工作委派给这个 Bot。",
    "config.section.attachedSubagents.title": "已附加的子代理",
    "config.section.attachedSubagents.subtitle": "把其他可作子代理的 Bot 添加到当前 Bot 的委派菜单中。",
    "config.subagents.attached": "已附加",
    "config.subagents.available": "可添加",
    "config.subagents.unavailableDescription": "不可用子代理",
    "config.subagents.unavailable": "当前不可用。请移除，或重新启用那个 Bot。",
    "config.subagents.emptyAttached": "还没有附加任何子代理。",
    "config.subagents.sourceBot": "来源 Bot：{name}",
    "config.subagents.emptyAvailable": "当前没有可用候选项。先在其他 Bot 上启用“允许作为子代理”。",
    "config.section.soul.title": "Soul",
    "config.section.soul.subtitle": "会注入到 Bot 的 system prompt 中。",
    "config.section.soul.placeholder": "描述这个 Bot 的角色、约束和工作风格...",
    "config.section.skills.title": "Skills",
    "config.section.skills.subtitle": "选择这个 Bot 可以使用哪些技能。",
    "config.skills.search": "搜索技能...",
    "config.skills.empty": "未找到技能。",
    "config.section.mcp.title": "MCP Servers",
    "config.section.mcp.subtitle": "按 Bot 覆盖 MCP 的启用状态。",
    "config.mcp.default":
      ({ enabled }) => `默认：${enabled === "true" ? "启用" : "禁用"}`,
    "config.mcp.inherit": "继承",
    "config.section.toolPlugins.title": "工具插件路径",
    "config.listEditor.empty": "还没有条目。",
    "config.section.advanced.title": "高级直连 LLM 设置",
    "config.section.advanced.subtitle": "当未选择共享聊天模型时使用。",
    "config.field.baseUrl": "Base URL",
    "config.field.fallbackModel": "后备模型",
    "config.field.apiKey": "API Key",
    "config.field.currentValue": "当前：{value}",
    "config.field.newApiKey": "粘贴新的 API Key",
    "config.section.providers.title": "Providers",
    "config.section.providers.subtitle": "供所有 Bot 共享的全局 Provider 注册表。",
    "config.provider.add": "添加 Provider",
    "config.providers.empty": "还没有 Provider。先添加一个，再开始导入模型。",
    "config.section.providerDetail.subtitle": "Provider 凭据和连接设置。",
    "config.provider.save": "保存 Provider",
    "config.provider.saving": "保存中...",
    "config.provider.name": "Provider 名称",
    "config.provider.enabled": "启用 Provider",
    "config.provider.enabled.subtitle": "禁用后会隐藏它挂载的全部模型。",
    "config.provider.newApiKey": "粘贴新的 Provider API Key",
    "config.section.models.title": "已注册模型",
    "config.section.models.subtitle": "为这个 Provider 启用、禁用、添加或移除模型。",
    "config.models.add": "添加模型",
    "config.models.selection":
      ({ selected, total }) => (Number(selected) > 0 ? `已选 ${selected} 个` : `已注册 ${total} 个`),
    "config.models.deleteSelected": "删除所选",
    "config.models.empty": "还没有添加模型。",
    "config.section.fetch.title": "从 /models 拉取",
    "config.section.fetch.subtitle": "拉取这个 Provider 公布的模型列表，并导入选中的条目。",
    "config.fetch.action": "拉取模型",
    "config.fetch.loading": "拉取中...",
    "config.fetch.search": "搜索已拉取模型...",
    "config.fetch.selection":
      ({ selected, available }) =>
        Number(selected) > 0 ? `已选 ${selected} 个` : `可导入 ${available} 个`,
    "config.fetch.clearVisible": "清空当前可见项",
    "config.fetch.addSelected": "添加所选",
    "config.fetch.importAll": "导入全部新模型",
    "config.fetch.alreadyAdded": "已添加",
    "config.fetch.availableToImport": "可导入",
    "config.fetch.emptySearch": "当前搜索条件下没有匹配的已拉取模型。",
    "config.fetch.empty": "先拉取模型，再查看可导入选项。",
    "config.providerDetail.title": "Provider 详情",
    "config.providerDetail.emptySubtitle": "选择一个 Provider 以查看其模型和凭据。",
    "config.providerDetail.empty": "尚未选择 Provider。",
    "config.providerDetail.current": "当前 Provider",
    "config.deleteModels.title":
      ({ count }) => (Number(count) === 1 ? "删除模型" : "删除模型"),
    "config.deleteModels.single":
      "从 {provider} 移除 {model}？使用这些模型的 Bot 需要重新选择聊天模型后才能再次发送消息。",
    "config.deleteModels.multi":
      "从 {provider} 移除这 {count} 个已选模型？使用这些模型的 Bot 需要重新选择聊天模型后才能再次发送消息。",
    "config.deleteModels.confirm": "删除模型",
    "config.deleteModels.deleting": "删除中...",
    "config.providerModal.title": "添加 Provider",
    "config.providerModal.subtitle": "配置一个共享的 OpenAI 兼容 Provider。",
    "config.providerModal.name.placeholder": "OpenAI / OpenRouter / Local",
    "config.providerModal.apiKey.placeholder": "Provider API Key",
    "config.providerModal.confirm": "创建 Provider",
    "config.providerModal.creating": "创建中...",
  },
  en: {
    "common.on": "On",
    "common.off": "Off",
    "common.enabled": "Enabled",
    "common.disabled": "Disabled",
    "common.inherit": "Inherit",
    "common.add": "Add",
    "common.remove": "Remove",
    "common.cancel": "Cancel",
    "common.create": "Create",
    "common.delete": "Delete",
    "common.refresh": "Refresh",
    "common.search": "Search",
    "common.none": "None",
    "common.notSet": "Not set",
    "common.unknown": "Unknown",
    "common.current": "Current",
    "common.selectAll": "Select All",
    "common.clearSelection": "Clear Selection",
    "common.save": "Save",
    "common.loading": "Loading",
    "common.available": "Available",
    "common.attached": "Attached",
    "common.default": "default",
    "app.title.chat": "Minibot • Chat",
    "app.title.config": "Minibot • Config",
    "app.brand": "Minibot",
    "app.tab.chat": "Chat",
    "app.tab.config": "Config",
    "app.bot.new": "New Bot",
    "app.bot.delete": "Delete",
    "app.bot.create.title": "Create Bot",
    "app.bot.create.description": "Give your bot a friendly name (optional).",
    "app.bot.create.label": "Bot name",
    "app.bot.create.placeholder": "e.g. Mini QA",
    "app.bot.create.confirm": "Create",
    "app.bot.delete.title": "Delete Bot",
    "app.bot.delete.description":
      "Delete {name} and all its sessions? This cannot be undone.",
    "language.button.title": "Language",
    "language.button.aria": "Language: {current}. Open language menu.",
    "language.menu.label": "Interface language",
    "language.option.zh": "中文",
    "language.option.zh.description": "Show the interface in Simplified Chinese",
    "language.option.en": "English",
    "language.option.en.description": "Show the interface in English",
    "theme.mode.light": "Light",
    "theme.mode.dark": "Dark",
    "theme.mode.system": "Auto",
    "theme.button.aria": "Theme: {current}. Click to switch to {next}.",
    "chat.tool.badge": "Tool",
    "chat.tool.arguments": "Arguments",
    "chat.tool.noArguments": "No arguments.",
    "chat.tool.result": "Result",
    "chat.tool.waiting": "Waiting for tool result...",
    "chat.tool.noResult": "No result.",
    "chat.tool.status.running": "Running",
    "chat.tool.status.done": "Done",
    "chat.tool.status.error": "Error",
    "chat.status.copiedReply": "Copied reply.",
    "chat.status.deletedTurn": "Deleted message turn.",
    "chat.status.regeneratedReply": "Regenerated latest reply.",
    "chat.status.chatModelUpdated": "Chat model updated.",
    "chat.status.usingFallbackModel": "Using fallback bot model.",
    "chat.fallbackDisabledReason": "This bot is disabled.",
    "chat.sessions.title": "Sessions",
    "chat.sessions.new": "New",
    "chat.sessions.refresh": "Refresh",
    "chat.sessions.noPreview": "(no preview)",
    "chat.sessions.deleteTitle": "Delete session",
    "chat.sessions.deleteShort": "Del",
    "chat.sessions.empty": "No sessions yet. Start a new one when this bot is ready for chat.",
    "chat.header.label": "Chat",
    "chat.header.session": "Session:",
    "chat.header.none": "(none)",
    "chat.header.disabled": "disabled",
    "chat.model.label": "Chat model",
    "chat.model.fallbackOption": "Fallback bot model",
    "chat.model.stop": "Stop",
    "chat.model.usingSelected": "Using {provider} / {label}",
    "chat.model.usingFallback":
      ({ model }) => (model ? `Using fallback model: ${model}` : "Using fallback model"),
    "chat.reasoning.title": "Deep Thinking",
    "chat.message.copy": "Copy",
    "chat.message.regenerate": "Regenerate",
    "chat.message.delete": "Delete",
    "chat.message.tokens": "Tokens {count}",
    "chat.empty.blocked": "Resolve the bot or model issue above before sending a message.",
    "chat.empty.ready": "Start by sending a message.",
    "chat.input.blocked": "Chat is unavailable for this bot.",
    "chat.input.ready": "Message Minibot...",
    "chat.input.send": "Send",
    "chat.input.hint": "Enter to send • Shift+Enter for newline",
    "chat.deleteSession.title": "Delete session",
    "chat.deleteSession.description":
      "Delete {sessionId} and remove its messages from this bot? This cannot be undone.",
    "config.status.botSaved": "Bot configuration saved.",
    "config.status.providerUpdated": "Provider updated.",
    "config.status.providerAdded": "Provider added.",
    "config.status.importedModels":
      ({ count }) => `Imported ${count} model${Number(count) === 1 ? "" : "s"}.`,
    "config.status.deletedModels":
      ({ count }) => `Deleted ${count} model${Number(count) === 1 ? "" : "s"}.`,
    "config.summary.providers": "Providers",
    "config.summary.models": "Models",
    "config.summary.bots": "Bots",
    "config.summary.subagents": "Subagents",
    "config.header.title": "Control Dashboard",
    "config.header.subtitle": "Manage providers, models, bots, and subagent relationships from one place.",
    "config.tab.bots": "Bots",
    "config.tab.providers": "Providers",
    "config.loading": "Loading dashboard...",
    "config.section.allBots.title": "All Bots",
    "config.section.allBots.subtitle": "Click a bot to make it the active editor target.",
    "config.bot.tag.default": "default",
    "config.bot.tag.subagentReady": "subagent-ready",
    "config.bot.chatUnavailable": "Chat unavailable",
    "config.section.botDetail.title":
      ({ name }) => (name ? `Editing ${name}` : "Bot Detail"),
    "config.section.botDetail.subtitle": "This panel controls the currently selected bot.",
    "config.bot.save": "Save Bot",
    "config.bot.saving": "Saving...",
    "config.bot.name": "Bot name",
    "config.bot.name.placeholder": "My Bot",
    "config.bot.enabled": "Bot enabled",
    "config.bot.enabled.subtitle": "Disabled bots cannot be used for chat or as subagents.",
    "config.bot.chatModel": "Chat model",
    "config.bot.chatModel.fallback": "Use advanced fallback model",
    "config.bot.streaming": "Streaming",
    "config.bot.streaming.subtitle": "Enable token-by-token output in chat.",
    "config.section.subagentIdentity.title": "Subagent Identity",
    "config.section.subagentIdentity.subtitle": "Control whether other bots can attach this bot as a subagent.",
    "config.bot.allowSubagent": "Allow as subagent",
    "config.bot.allowSubagent.subtitle": "Makes this bot appear in other bots' subagent picker.",
    "config.bot.subagentDisplayName": "Subagent display name",
    "config.bot.subagentDescription": "Subagent description",
    "config.bot.subagentDescription.placeholder": "Describe when this bot should be delegated work.",
    "config.section.attachedSubagents.title": "Attached Subagents",
    "config.section.attachedSubagents.subtitle": "Add other subagent-ready bots to this bot's delegation menu.",
    "config.subagents.attached": "Attached",
    "config.subagents.available": "Available to add",
    "config.subagents.unavailableDescription": "Unavailable subagent",
    "config.subagents.unavailable": "Unavailable now. Remove or re-enable that bot.",
    "config.subagents.emptyAttached": "No subagents attached yet.",
    "config.subagents.sourceBot": "Source bot: {name}",
    "config.subagents.emptyAvailable": 'No eligible bots available. Enable "Allow as subagent" on another bot first.',
    "config.section.soul.title": "Soul",
    "config.section.soul.subtitle": "Injected into the bot's system prompt.",
    "config.section.soul.placeholder": "Describe this bot's persona, constraints, and operating style...",
    "config.section.skills.title": "Skills",
    "config.section.skills.subtitle": "Choose which skills this bot can use.",
    "config.skills.search": "Search skills...",
    "config.skills.empty": "No skills found.",
    "config.section.mcp.title": "MCP Servers",
    "config.section.mcp.subtitle": "Override MCP enablement per bot.",
    "config.mcp.default":
      ({ enabled }) => `default: ${enabled === "true" ? "enabled" : "disabled"}`,
    "config.mcp.inherit": "Inherit",
    "config.section.toolPlugins.title": "Tool Plugin Paths",
    "config.listEditor.empty": "No items yet.",
    "config.section.advanced.title": "Advanced Direct LLM Settings",
    "config.section.advanced.subtitle": "Used when no shared chat model is selected.",
    "config.field.baseUrl": "Base URL",
    "config.field.fallbackModel": "Fallback model",
    "config.field.apiKey": "API Key",
    "config.field.currentValue": "Current: {value}",
    "config.field.newApiKey": "Paste a new API key",
    "config.section.providers.title": "Providers",
    "config.section.providers.subtitle": "Global provider registry for all bots.",
    "config.provider.add": "Add Provider",
    "config.providers.empty": "No providers yet. Add one to start importing models.",
    "config.section.providerDetail.subtitle": "Provider credentials and connection settings.",
    "config.provider.save": "Save Provider",
    "config.provider.saving": "Saving...",
    "config.provider.name": "Provider name",
    "config.provider.enabled": "Provider enabled",
    "config.provider.enabled.subtitle": "Disabled providers hide all attached models.",
    "config.provider.newApiKey": "Paste a new provider API key",
    "config.section.models.title": "Registered Models",
    "config.section.models.subtitle": "Enable, disable, add, or remove models for this provider.",
    "config.models.add": "Add Model",
    "config.models.selection":
      ({ selected, total }) => (Number(selected) > 0 ? `${selected} selected` : `${total} registered`),
    "config.models.deleteSelected": "Delete Selected",
    "config.models.empty": "No models added yet.",
    "config.section.fetch.title": "Fetch From /models",
    "config.section.fetch.subtitle": "Pull the provider's advertised model list and import selected entries.",
    "config.fetch.action": "Fetch Models",
    "config.fetch.loading": "Fetching...",
    "config.fetch.search": "Search fetched models...",
    "config.fetch.selection":
      ({ selected, available }) =>
        Number(selected) > 0 ? `${selected} selected` : `${available} available to import`,
    "config.fetch.clearVisible": "Clear Visible",
    "config.fetch.addSelected": "Add Selected",
    "config.fetch.importAll": "Import All New",
    "config.fetch.alreadyAdded": "Already added",
    "config.fetch.availableToImport": "Available to import",
    "config.fetch.emptySearch": "No fetched models match the current search.",
    "config.fetch.empty": "Fetch models to see import options.",
    "config.providerDetail.title": "Provider Detail",
    "config.providerDetail.emptySubtitle": "Select a provider to inspect its models and credentials.",
    "config.providerDetail.empty": "No provider selected.",
    "config.providerDetail.current": "this provider",
    "config.deleteModels.title":
      ({ count }) => (Number(count) === 1 ? "Delete model" : "Delete models"),
    "config.deleteModels.single":
      "Remove {model} from {provider}? Bots using this model will need a new chat model before they can send messages again.",
    "config.deleteModels.multi":
      "Remove {count} selected models from {provider}? Bots using these models will need a new chat model before they can send messages again.",
    "config.deleteModels.confirm": "Delete Models",
    "config.deleteModels.deleting": "Deleting...",
    "config.providerModal.title": "Add Provider",
    "config.providerModal.subtitle": "Configure a shared OpenAI-compatible provider.",
    "config.providerModal.name.placeholder": "OpenAI / OpenRouter / Local",
    "config.providerModal.apiKey.placeholder": "Provider API key",
    "config.providerModal.confirm": "Create Provider",
    "config.providerModal.creating": "Creating...",
  },
};

function interpolate(template: string, params: TranslationParams = {}) {
  return template.replace(/\{(\w+)\}/g, (_, key: string) => {
    const value = params[key];
    return value == null ? "" : String(value);
  });
}

function formatEntry(entry: TranslationEntry, params?: TranslationParams) {
  if (typeof entry === "function") {
    return entry(params ?? {});
  }
  return interpolate(entry, params);
}

export function parseLanguage(value: string | null): Language {
  return value === "en" || value === "zh-CN" ? value : "zh-CN";
}

export function createTranslator(language: Language): Translator {
  return (key, params) => {
    const entry = messages[language][key] ?? messages.en[key];
    if (!entry) return key;
    return formatEntry(entry, params);
  };
}

type I18nContextValue = {
  language: Language;
  setLanguage: (language: Language) => void;
  t: Translator;
};

const I18nContext = createContext<I18nContextValue | null>(null);

export function I18nProvider(props: { value: I18nContextValue; children: ReactNode }) {
  return <I18nContext.Provider value={props.value}>{props.children}</I18nContext.Provider>;
}

export function useI18n() {
  const value = useContext(I18nContext);
  if (!value) {
    throw new Error("useI18n must be used within an I18nProvider.");
  }
  return value;
}
