import path from 'path';

import { outputFileSync, readdirSync, readFileSync, statSync } from 'fs-extra';
import { createStream } from 'table';

let project = process.argv.find((arg) => arg.includes('--project'));
if (project) {
  project = project.slice('--project='.length);
}
const ROOT_PATH = path.join(__dirname, '..', project ? `packages${path.sep}${project}` : '.');
const OUT_FILE = 'base64.out.ts';

const table = createStream({
  columnDefault: { width: 60 },
  columnCount: 2,
  columns: [{ width: 20 }, { width: 40 }],
});

const reduceDir = (dirPath: string, paths: string[] = []) => {
  let output = '';
  const files = readdirSync(dirPath);
  for (const file of files) {
    const filePath = path.join(dirPath, file);
    if (statSync(filePath).isDirectory()) {
      reduceDir(filePath, [...paths, file]);
    } else if (/^base64\.[\s\S]+\.[\s\S]+$/.test(file) && file !== OUT_FILE) {
      const filePathReg = new RegExp(`(?<=packages\\${path.sep})[\\s\\S]+?(?=\\${path.sep})`);
      const targetPath = filePath.match(filePathReg) as string[];
      table.write([targetPath?.[0], file]);
      const bitmap = readFileSync(filePath, { encoding: 'base64' });
      const fileMiddleName = file.match(/(?<=\.)[\s\S]+(?=\.)/) as string[];
      output += String.raw`  '${fileMiddleName?.[0]}': '${bitmap}',
`;
    }
  }
  if (output.length > 0) {
    outputFileSync(
      path.join(dirPath, OUT_FILE),
      String.raw`/* eslint-disable */
// @ts-nocheck

export const BASE64_DATA = {
${output}};
`
    );
  }
};
reduceDir(ROOT_PATH);