/* ===== i18n.jsx — 中文 / English / Italiano ===== */
(function () {
  const dict = {
    zh: {
      // App-level
      app_title: '实验仓 ERP',
      app_subtitle: 'Phase 1',
      lang_zh: '中文',
      lang_en: 'English',
      lang_it: 'Italiano',
      // Nav
      nav_dashboard: '仪表盘',
      nav_alerts: '全部报警',
      nav_suppliers: '供货商',
      nav_customers: '客户',
      nav_reagents: '试剂',
      nav_reagent_products: '产品目录',
      nav_reagent_batches: '批次',
      nav_reagent_outflow_new: '取货',
      nav_devices: '设备',
      nav_device_products: '型号目录',
      nav_device_units: '设备列表',
      nav_device_transfer_new: '新转移',
      nav_settings: '系统',
      nav_settings_users: '用户管理',
      nav_settings_thr: '低库存阈值',
      // Generic
      g_new: '新建',
      g_edit: '编辑',
      g_save: '保存',
      g_save_continue: '保存并新建下一条',
      g_cancel: '取消',
      g_delete: '删除',
      g_archive: '归档',
      g_restore: '还原',
      g_search: '搜索',
      g_filter: '筛选',
      g_required: '必填',
      g_yes: '是',
      g_no: '否',
      g_loading: '加载中…',
      g_empty: '暂无数据',
      g_error: '出错了',
      g_close: '关闭',
      g_back: '返回',
      g_login: '登录',
      g_logout: '退出登录',
      // Auth
      auth_email: '邮箱',
      auth_password: '密码',
      auth_signin: '登录',
      // Dashboard
      d_alarms: '报警',
      d_recent: '最近活动',
      // Alarms
      al_expired: '已过期',
      al_expiring: '即将过期',
      al_low_stock: '低库存',
      al_outflow_over: '取货超期',
      al_stale: '滞库',
      al_device_fault: '设备故障',
      al_repair_over: '修复超期',
      // Reagent fields
      r_lot: '批号',
      r_expiry: '保质期',
      r_boxes: '盒',
      r_tests: 'test',
      r_received: '入库',
      r_remaining: '剩余',
      r_unit_kit: '整盒',
      r_unit_test: '单个 test',
      r_destination: '目的地',
      // Device fields
      dv_sn: '序列号',
      dv_model: '型号',
      dv_status: '状态',
      dv_in_stock: '在库',
      dv_at_customer: '在客户',
      dv_fault: '故障',
      dv_holder: '当前持有方',
      dv_timeline: '转移时间轴',
      // PWA
      pwa_install: '安装到桌面',
    },
    en: {
      app_title: 'Lab ERP',
      app_subtitle: 'Phase 1',
      lang_zh: '中文',
      lang_en: 'English',
      lang_it: 'Italiano',
      nav_dashboard: 'Dashboard',
      nav_alerts: 'All alerts',
      nav_suppliers: 'Suppliers',
      nav_customers: 'Customers',
      nav_reagents: 'Reagents',
      nav_reagent_products: 'Catalog',
      nav_reagent_batches: 'Batches',
      nav_reagent_outflow_new: 'Withdraw',
      nav_devices: 'Devices',
      nav_device_products: 'Models',
      nav_device_units: 'Units',
      nav_device_transfer_new: 'Transfer',
      nav_settings: 'Settings',
      nav_settings_users: 'Users',
      nav_settings_thr: 'Stock thresholds',
      g_new: 'New',
      g_edit: 'Edit',
      g_save: 'Save',
      g_save_continue: 'Save & new',
      g_cancel: 'Cancel',
      g_delete: 'Delete',
      g_archive: 'Archive',
      g_restore: 'Restore',
      g_search: 'Search',
      g_filter: 'Filter',
      g_required: 'Required',
      g_yes: 'Yes',
      g_no: 'No',
      g_loading: 'Loading…',
      g_empty: 'No data',
      g_error: 'Error',
      g_close: 'Close',
      g_back: 'Back',
      g_login: 'Sign in',
      g_logout: 'Sign out',
      auth_email: 'Email',
      auth_password: 'Password',
      auth_signin: 'Sign in',
      d_alarms: 'Alerts',
      d_recent: 'Recent activity',
      al_expired: 'Expired',
      al_expiring: 'Expiring soon',
      al_low_stock: 'Low stock',
      al_outflow_over: 'Overdue withdrawal',
      al_stale: 'Stale',
      al_device_fault: 'Device fault',
      al_repair_over: 'Repair overdue',
      r_lot: 'Lot',
      r_expiry: 'Expiry',
      r_boxes: 'box',
      r_tests: 'test',
      r_received: 'Received',
      r_remaining: 'On hand',
      r_unit_kit: 'Box',
      r_unit_test: 'Single test',
      r_destination: 'Destination',
      dv_sn: 'Serial',
      dv_model: 'Model',
      dv_status: 'Status',
      dv_in_stock: 'In stock',
      dv_at_customer: 'At customer',
      dv_fault: 'Fault',
      dv_holder: 'Current holder',
      dv_timeline: 'Transfer timeline',
      pwa_install: 'Install app',
    },
    it: {
      app_title: 'ERP Laboratorio',
      app_subtitle: 'Fase 1',
      lang_zh: '中文',
      lang_en: 'English',
      lang_it: 'Italiano',
      nav_dashboard: 'Dashboard',
      nav_alerts: 'Tutti gli avvisi',
      nav_suppliers: 'Fornitori',
      nav_customers: 'Clienti',
      nav_reagents: 'Reagenti',
      nav_reagent_products: 'Catalogo',
      nav_reagent_batches: 'Lotti',
      nav_reagent_outflow_new: 'Prelievo',
      nav_devices: 'Strumenti',
      nav_device_products: 'Modelli',
      nav_device_units: 'Unità',
      nav_device_transfer_new: 'Trasferimento',
      nav_settings: 'Impostazioni',
      nav_settings_users: 'Utenti',
      nav_settings_thr: 'Soglie scorta',
      g_new: 'Nuovo',
      g_edit: 'Modifica',
      g_save: 'Salva',
      g_save_continue: 'Salva e nuovo',
      g_cancel: 'Annulla',
      g_delete: 'Elimina',
      g_archive: 'Archivia',
      g_restore: 'Ripristina',
      g_search: 'Cerca',
      g_filter: 'Filtra',
      g_required: 'Obbligatorio',
      g_yes: 'Sì',
      g_no: 'No',
      g_loading: 'Caricamento…',
      g_empty: 'Nessun dato',
      g_error: 'Errore',
      g_close: 'Chiudi',
      g_back: 'Indietro',
      g_login: 'Accedi',
      g_logout: 'Esci',
      auth_email: 'Email',
      auth_password: 'Password',
      auth_signin: 'Accedi',
      d_alarms: 'Avvisi',
      d_recent: 'Attività recente',
      al_expired: 'Scaduto',
      al_expiring: 'In scadenza',
      al_low_stock: 'Scorta bassa',
      al_outflow_over: 'Prelievo scaduto',
      al_stale: 'Inattivo',
      al_device_fault: 'Guasto',
      al_repair_over: 'Riparazione lunga',
      r_lot: 'Lotto',
      r_expiry: 'Scadenza',
      r_boxes: 'scatola',
      r_tests: 'test',
      r_received: 'Ricevuto',
      r_remaining: 'Disponibile',
      r_unit_kit: 'Scatola',
      r_unit_test: 'Singolo test',
      r_destination: 'Destinatario',
      dv_sn: 'Matricola',
      dv_model: 'Modello',
      dv_status: 'Stato',
      dv_in_stock: 'In magazzino',
      dv_at_customer: 'Presso cliente',
      dv_fault: 'Guasto',
      dv_holder: 'Detentore',
      dv_timeline: 'Cronologia trasferimenti',
      pwa_install: 'Installa app',
    },
  };

  const LANG_KEY = 'erp.lang';
  function getLang() {
    const saved = localStorage.getItem(LANG_KEY);
    if (saved && dict[saved]) return saved;
    const browser = (navigator.language || 'zh').toLowerCase();
    if (browser.startsWith('it')) return 'it';
    if (browser.startsWith('zh')) return 'zh';
    if (browser.startsWith('en')) return 'en';
    return 'zh';
  }
  function setLang(l) {
    if (!dict[l]) return;
    localStorage.setItem(LANG_KEY, l);
    document.documentElement.lang = l === 'zh' ? 'zh-CN' : l;
    window.dispatchEvent(new CustomEvent('erp:langchange', { detail: l }));
  }
  function t(key, fallback) {
    const lang = getLang();
    return (dict[lang] && dict[lang][key]) || (dict.zh && dict.zh[key]) || fallback || key;
  }

  // React hook
  function useT() {
    const [, setTick] = React.useState(0);
    React.useEffect(() => {
      const on = () => setTick((x) => x + 1);
      window.addEventListener('erp:langchange', on);
      return () => window.removeEventListener('erp:langchange', on);
    }, []);
    return { t, lang: getLang(), setLang };
  }

  // Init html lang on load
  document.documentElement.lang = getLang() === 'zh' ? 'zh-CN' : getLang();

  Object.assign(window, { t, useT, getLang, setLang, langDict: dict });
})();
