import { consola } from 'consola';
import { colors } from 'consola/utils';
import { existsSync, mkdirSync } from 'node:fs';
import { dirname } from 'node:path';

import { entryLocaleJsonFilepath, i18nConfig, localeDir, srcDefaultLocales } from './const';
import { tagWhite, writeJSON } from './utils';

export const genDefaultLocale = () => {
  consola.info(`Default locale: ${i18nConfig.entryLocale}...`);

  // Ensure entry locale directory exists
  const entryLocaleDir = localeDir(i18nConfig.entryLocale);
  if (!existsSync(entryLocaleDir)) {
    mkdirSync(entryLocaleDir, { recursive: true });
    consola.info(`Creating directory: ${entryLocaleDir}`);
  }

  const resources = require(srcDefaultLocales);
  const data = Object.entries(resources.default);
  consola.start(`Generating default locale JSON files, found ${data.length} namespaces...`);

  for (const [ns, value] of data) {
    const filepath = entryLocaleJsonFilepath(`${ns}.json`);

    // Ensure directory exists
    const dir = dirname(filepath);
    if (!existsSync(dir)) {
      mkdirSync(dir, { recursive: true });
    }

    writeJSON(filepath, value);
    consola.success(tagWhite(ns), colors.gray(filepath));
  }
};
