import { readdirSync, statSync, readFileSync, writeFileSync } from 'fs';
import { join, relative } from 'path';

const distDir = './dist';
const outputFile = './embedded-static.js';

function walkDir(dir, baseDir, result = {}) {
  const items = readdirSync(dir);
  for (const item of items) {
    const fullPath = join(dir, item);
    const stat = statSync(fullPath);
    if (stat.isDirectory()) {
      walkDir(fullPath, baseDir, result);
    } else {
      const relPath = relative(baseDir, fullPath).replace(/\\/g, '/');
      const content = readFileSync(fullPath);
      result[relPath] = content.toString('base64');
    }
  }
  return result;
}

console.log('Embedding static files...');
const files = walkDir(distDir, distDir);
console.log('Found', Object.keys(files).length, 'files in dist');

const jsContent = `// Auto-generated - embedded static files
export const staticFiles = ${JSON.stringify(files, null, 0)};
export const fileCount = ${Object.keys(files).length};
`;

writeFileSync(outputFile, jsContent);
console.log('Written to', outputFile, 'size:', statSync(outputFile).size);
