const TRANSLATIONS = {
  zh: {
    title: "Sensa 基础版",
    description:
      "Sensa 基础版是一款 Chrome 侧边栏网页 AI 助手，支持读取当前网页内容、附带页面截图，并与大模型进行上下文对话。",
    eyebrow: "Sensa Basic",
    heroSubtitle: "让 AI 理解当前网页，而不是只理解你的输入框。",
    heroText:
      "Sensa 基础版运行在 Chrome 侧边栏中。它可以读取当前网页内容、理解页面结构，并把页面截图一起发送给大模型，让回答建立在你此刻正在浏览的真实上下文之上。",
    downloadZip: "下载插件 ZIP",
    viewManual: "查看使用手册",
    heroNote: "适合愿意手动开启 Chrome 开发者模式并加载插件的用户。",
    coreLabel: "核心理念",
    coreItem1: "不是另一个聊天框，而是网页理解助手",
    coreItem2: "默认支持页面文本和页面截图双上下文",
    coreItem3: "同页连续提问支持附件去重，减少重复消耗",
    coreItem4: "页面级会话历史、诊断导出、手动停止生成",
    capabilityKicker: "产品能力",
    capabilityTitle: "基础版保留了最有价值的网页上下文能力。",
    featureReadTitle: "读取当前网页",
    featureReadText: "抓取标题、正文、结构化信息、按钮与可见交互元素。",
    featureShotTitle: "附带页面截图",
    featureShotText: "把你正在看的界面一起发送给模型，减少“只看文字”的理解偏差。",
    featureHistoryTitle: "页面级历史",
    featureHistoryText: "不同网页的会话分开保存，同一页面连续追问时上下文更自然。",
    featureDedupeTitle: "智能去重",
    featureDedupeText: "页面内容与截图没变化时，不重复发送相同附件，节省输入消耗。",
    installKicker: "安装说明",
    installTitle: "三步完成本地安装。",
    installStep1: "下载并解压 <code>Sensa_basic_v1.0.1.zip</code>。",
    installStep2: "打开 <code>chrome://extensions</code>，启用开发者模式。",
    installStep3: "点击“加载已解压的扩展程序”，选择解压后的 <code>Sensa_basic</code> 文件夹。",
    viewFullManual: "查看完整使用手册",
    sceneKicker: "适用场景",
    sceneTitle: "更适合后台、配置页、复杂页面，而不是泛泛聊天。",
    sceneItem1: "解释平台后台中的配置区域",
    sceneItem2: "总结长网页、表格、表单页的关键信息",
    sceneItem3: "结合截图分析界面布局与操作步骤",
    sceneItem4: "在同一页面上连续追问并保留上下文",
    dataKicker: "数据说明",
    dataTitle: "使用前需要理解的数据边界。",
    dataItem1: "当前网页内容可能会发送到你配置的大模型接口",
    dataItem2: "当前页面截图也可能会作为图片输入发送出去",
    dataItem3: "本地会保存会话历史、设置与诊断数据",
    dataItem4: "不建议直接用于高敏感页面，除非你确认自己的接口与数据流向",
    contactKicker: "联系作者",
    contactTitle: "如果你想交流产品或反馈体验，可以在小红书找到我。",
    contactName: "账号名称：Ruska 的 AI实验室",
    contactHandle: "小红书号：<code>ruskacoffee</code>",
    contactCaption: "扫描二维码，在小红书联系我",
    footerName: "Sensa 基础版",
    footerDownload: "下载 ZIP",
    footerManual: "使用手册",
    footerStats: "站点统计"
  },
  en: {
    title: "Sensa Basic",
    description:
      "Sensa Basic is a Chrome side panel assistant that reads the current page, can attach screenshots, and chats with your model in real page context.",
    eyebrow: "Sensa Basic",
    heroSubtitle: "Help AI understand the page you are on, not just the prompt box.",
    heroText:
      "Sensa Basic runs inside the Chrome side panel. It reads the current page, understands page structure, and can send a screenshot together with your prompt so the model responds against the real context you are browsing.",
    downloadZip: "Download ZIP",
    viewManual: "Open Manual",
    heroNote: "Best for users who are comfortable enabling Chrome developer mode and loading an unpacked extension.",
    coreLabel: "Core Ideas",
    coreItem1: "Not another chat box, but a page understanding assistant",
    coreItem2: "Supports both page text and page screenshots by default",
    coreItem3: "Deduplicates unchanged attachments across follow-up turns",
    coreItem4: "Page-level history, diagnostics export, and manual stop",
    capabilityKicker: "Capabilities",
    capabilityTitle: "The basic edition keeps the most valuable page-context abilities.",
    featureReadTitle: "Read the current page",
    featureReadText: "Captures titles, body text, structured data, buttons, and visible interactive elements.",
    featureShotTitle: "Attach page screenshots",
    featureShotText: "Sends the screen you are looking at to the model and reduces text-only misunderstanding.",
    featureHistoryTitle: "Page-level history",
    featureHistoryText: "Conversations are stored per page, so follow-up questions on the same page feel more natural.",
    featureDedupeTitle: "Smart dedupe",
    featureDedupeText: "If the page and screenshot have not changed, the same attachments are not sent again.",
    installKicker: "Install",
    installTitle: "Finish local installation in three steps.",
    installStep1: "Download and unzip <code>Sensa_basic_v1.0.1.zip</code>.",
    installStep2: "Open <code>chrome://extensions</code> and enable Developer mode.",
    installStep3: "Click Load unpacked and choose the unzipped <code>Sensa_basic</code> folder.",
    viewFullManual: "View Full Manual",
    sceneKicker: "Best For",
    sceneTitle: "Better for admin panels, configuration pages, and dense interfaces than generic chatting.",
    sceneItem1: "Explain configuration areas in internal tools",
    sceneItem2: "Summarize long pages, tables, and form-heavy screens",
    sceneItem3: "Analyze layouts and next steps with screenshots",
    sceneItem4: "Ask follow-up questions on the same page with preserved context",
    dataKicker: "Data Boundary",
    dataTitle: "Understand the data flow before using it.",
    dataItem1: "Current page content may be sent to the model endpoint you configure",
    dataItem2: "Current page screenshots may also be sent as image input",
    dataItem3: "Conversation history, settings, and diagnostics are stored locally",
    dataItem4: "Avoid highly sensitive pages unless you fully trust your endpoint and data path",
    contactKicker: "Contact",
    contactTitle: "If you want to talk product or share feedback, you can find me on Xiaohongshu.",
    contactName: "Account: Ruska's AI Lab",
    contactHandle: "Xiaohongshu ID: <code>ruskacoffee</code>",
    contactCaption: "Scan the QR code to reach me on Xiaohongshu",
    footerName: "Sensa Basic",
    footerDownload: "Download ZIP",
    footerManual: "Manual",
    footerStats: "Stats"
  }
};

const STORAGE_KEY = "sensa-site-language";

function applyLanguage(lang) {
  const dictionary = TRANSLATIONS[lang] || TRANSLATIONS.zh;
  document.documentElement.lang = lang === "en" ? "en" : "zh-CN";
  document.title = dictionary.title;

  const metaDescription = document.querySelector('meta[name="description"]');
  if (metaDescription) {
    metaDescription.setAttribute("content", dictionary.description);
  }

  document.querySelectorAll("[data-i18n]").forEach((node) => {
    const key = node.getAttribute("data-i18n");
    if (key && dictionary[key]) {
      node.textContent = dictionary[key];
    }
  });

  document.querySelectorAll("[data-i18n-html]").forEach((node) => {
    const key = node.getAttribute("data-i18n-html");
    if (key && dictionary[key]) {
      node.innerHTML = dictionary[key];
    }
  });

  document.querySelectorAll("[data-lang]").forEach((button) => {
    button.classList.toggle("is-active", button.getAttribute("data-lang") === lang);
  });
}

function getInitialLanguage() {
  const stored = window.localStorage.getItem(STORAGE_KEY);
  if (stored === "zh" || stored === "en") {
    return stored;
  }
  return "zh";
}

document.addEventListener("DOMContentLoaded", () => {
  const initialLanguage = getInitialLanguage();
  applyLanguage(initialLanguage);

  document.querySelectorAll("[data-lang]").forEach((button) => {
    button.addEventListener("click", () => {
      const lang = button.getAttribute("data-lang") || "zh";
      window.localStorage.setItem(STORAGE_KEY, lang);
      applyLanguage(lang);
    });
  });
});
