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

function walk(dir, acc = []) {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const full = path.join(dir, entry.name);
    if (entry.isDirectory() && entry.name !== 'node_modules') {
      walk(full, acc);
    } else if (/\.(ts|vue)$/.test(entry.name)) {
      acc.push(full);
    }
  }
  return acc;
}

const srcFiles = walk('src');
const badPlaceholder = [];
const zhLocales = srcFiles.filter((f) => f.includes(`${path.sep}locale${path.sep}zh-CN.ts`));

for (const file of srcFiles) {
  const text = fs.readFileSync(file, 'utf8');
  if (text.includes("'???'") || /'(\?\?[^']*)'/.test(text)) {
    badPlaceholder.push(file);
  }
}

let zhOk = 0;
for (const file of zhLocales) {
  if (/[\u4e00-\u9fff]/.test(fs.readFileSync(file, 'utf8'))) zhOk += 1;
}

let mockInDist = false;
if (fs.existsSync('dist/assets')) {
  for (const name of fs.readdirSync('dist/assets')) {
    if (!name.endsWith('.js')) continue;
    const chunk = fs.readFileSync(path.join('dist/assets', name), 'utf8');
    if (chunk.includes('mockjs') || chunk.includes('Mock.mock')) {
      mockInDist = true;
      break;
    }
  }
}

console.log(JSON.stringify({
  badPlaceholder: badPlaceholder.length,
  zhLocales: `${zhOk}/${zhLocales.length}`,
  mockInDist,
  hasGit: fs.existsSync('.env.development') && fs.readFileSync('.env.development', 'utf8').includes('VITE_API_BASE_URL=http'),
  settingsHttp: fs.readFileSync('src/locale/zh-CN/settings.ts', 'utf8').includes('http.logout.title'),
  rootMenu: fs.readFileSync('src/locale/zh-CN.ts', 'utf8').includes('仪表盘'),
}, null, 2));
