import fs from 'node:fs';
import path from 'node:path';

const BASE =
  'https://raw.githubusercontent.com/arco-design/arco-design-pro-vue/main/arco-design-pro-vite/src';

const localeFiles = [
  'locale/zh-CN/settings.ts',
  'views/login/locale/zh-CN.ts',
  'views/form/group/locale/zh-CN.ts',
  'views/form/step/locale/zh-CN.ts',
  'views/dashboard/workplace/locale/zh-CN.ts',
  'views/dashboard/monitor/locale/zh-CN.ts',
  'views/list/card/locale/zh-CN.ts',
  'views/list/search-table/locale/zh-CN.ts',
  'views/profile/basic/locale/zh-CN.ts',
  'views/result/success/locale/zh-CN.ts',
  'views/result/error/locale/zh-CN.ts',
  'views/exception/403/locale/zh-CN.ts',
  'views/exception/404/locale/zh-CN.ts',
  'views/user/info/locale/zh-CN.ts',
  'views/user/setting/locale/zh-CN.ts',
  'views/visualization/data-analysis/locale/zh-CN.ts',
  'views/visualization/multi-dimension-data-analysis/locale/zh-CN.ts',
];

const rootZhCN = `import { mergeLocaleModules } from './merge-locales';
import localeSettings from './zh-CN/settings';

const componentLocales = mergeLocaleModules(
  import.meta.glob('@/components/**/locale/zh-CN.ts', { eager: true }),
);
const viewLocales = mergeLocaleModules(
  import.meta.glob('@/views/**/locale/zh-CN.ts', { eager: true }),
);

export default {
  'menu.dashboard': '仪表盘',
  'menu.server.dashboard': '仪表盘-服务端',
  'menu.server.workplace': '工作台-服务端',
  'menu.server.monitor': '实时监控-服务端',
  'menu.list': '列表页',
  'menu.result': '结果页',
  'menu.exception': '异常页',
  'menu.form': '表单页',
  'menu.profile': '详情页',
  'menu.visualization': '数据可视化',
  'menu.user': '个人中心',
  'menu.arcoWebsite': 'Arco Design',
  'menu.faq': '常见问题',
  'navbar.docs': '文档中心',
  'navbar.action.locale': '切换为中文',
  ...localeSettings,
  ...componentLocales,
  ...viewLocales,
};
`;

async function download(relPath) {
  const url = `${BASE}/${relPath}`;
  const res = await fetch(url);
  if (!res.ok) {
    throw new Error(`${relPath}: HTTP ${res.status}`);
  }
  return res.text();
}

async function main() {
  for (const relPath of localeFiles) {
    const content = await download(relPath);
    const dest = path.join('src', relPath.replace(/^locale\//, 'locale/'));
    const fullPath = path.join('src', relPath);
    fs.mkdirSync(path.dirname(fullPath), { recursive: true });
    fs.writeFileSync(fullPath, content, 'utf8');
    const hasCn = /[\u4e00-\u9fff]/.test(content);
    console.log(`OK ${relPath} (cn=${hasCn})`);
  }

  fs.writeFileSync(path.join('src', 'locale/zh-CN.ts'), rootZhCN, 'utf8');
  console.log('OK locale/zh-CN.ts (cn=true)');

  // search-table column setting label
  const stPath = path.join('src', 'views/list/search-table/index.vue');
  let st = fs.readFileSync(stPath, 'utf8');
  st = st.replace(
    "{{ item.title === '#' ? '???' : item.title }}",
    "{{ item.title === '#' ? '序列号' : item.title }}",
  );
  fs.writeFileSync(stPath, st, 'utf8');
  console.log('OK search-table/index.vue');

  // verify
  let bad = 0;
  for (const relPath of ['locale/zh-CN.ts', ...localeFiles]) {
    const fullPath = path.join('src', relPath);
    const text = fs.readFileSync(fullPath, 'utf8');
    if (text.includes("'???'") || text.includes("'??'")) {
      console.error('STILL BAD:', relPath);
      bad += 1;
    }
  }
  if (bad) process.exit(1);
  console.log('All locale files verified');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
