// import allTrans from './src/locales/lang/zh-CN/**/*.ts';
import { recursiveReaddir } from 'https://deno.land/x/recursive_readdir/mod.ts';
import { join, dirname, extname, basename } from 'https://deno.land/std/path/mod.ts';
import { flatten } from 'https://deno.land/x/flatten@1.1.0/mod.ts';

const currentPath = 'src/locales/lang/zh-CN';
const currentLang = 'zh-CN';
const transFiles = (await recursiveReaddir(join('.', currentPath))).filter(
  (file: string) => extname(file) === '.ts',
);

const allTrans = transFiles.map(async (src) => {
  const mod = (await import(`./${src}`)).default;

  const surfix = extname(src);
  const fileName = basename(src, surfix);
  const suFolder = basename(dirname(src));
  const data = {};
  if (suFolder == currentLang) {
    {
      data[fileName] = mod;
    }
  } else {
    const sub = {};
    sub[fileName] = mod;
    data[suFolder] = sub;
  }

  return data;
});

const obj = await Promise.all(allTrans);

const lastObj = obj.map((item) => flatten(item));

const last = Object.assign({}, ...lastObj);

await Deno.writeTextFile(`${currentLang}.json`, JSON.stringify(last));
