import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { gzipSync } from 'zlib';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const dist = path.join(__dirname, '../dist/assets');

if (!fs.existsSync(dist)) {
  console.error('dist/assets not found — run pnpm build first');
  process.exit(1);
}

const files = fs
  .readdirSync(dist)
  .filter((f) => f.endsWith('.js'))
  .map((f) => {
    const buf = fs.readFileSync(path.join(dist, f));
    return {
      file: f,
      raw: buf.length,
      gzip: gzipSync(buf).length,
    };
  })
  .sort((a, b) => b.gzip - a.gzip);

console.log('Admin bundle chunk sizes (gzip):');
for (const row of files) {
  console.log(`${(row.gzip / 1024).toFixed(1)} KB gzip  ${(row.raw / 1024).toFixed(1)} KB raw  ${row.file}`);
}
const totalGzip = files.reduce((s, r) => s + r.gzip, 0);
console.log(`\nTotal JS (all chunks): ${(totalGzip / 1024).toFixed(1)} KB gzip`);
