"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initialHTMLHandling = initialHTMLHandling;

var _buildToolLoggers = require("../../buildToolLoggers");

function initialHTMLHandling(compiler, app, options) {
  const {
    contextURL
  } = options;
  const {
    i18nChunkSplit
  } = options;
  const {
    rtlSplit
  } = options.css.plugins;
  let initalHTML;
  compiler.hooks.afterCompile.tap('ReactCLI', compilation => {
    const indexHtml = compilation.assets['index.html']; // compiler.outputFileSystem.readFileSync("build/index.html");

    if (indexHtml) {
      initalHTML = indexHtml.source();
    }
  });
  app.use(`${contextURL}/*`, (req, res) => {
    // // app.use(`/desk/*`, (req, res) => {
    // const { rtlSplit } = options.css.plugins;
    let newHTML = initalHTML;

    if (i18nChunkSplit.chunkSplitEnable) {
      // eslint-disable-next-line no-use-before-define
      newHTML = replaceWithRandomLocale(newHTML, i18nChunkSplit.templateLabel);
    }

    if (rtlSplit.enableRTLSplit) {
      // eslint-disable-next-line no-use-before-define
      newHTML = replaceWithRandomDir(newHTML, rtlSplit.templateLabel);
    }

    res.send(newHTML);
  });

  function replaceWithRandomLocale(initalHTML, templateLabel) {
    const locales = ['ar_EG', 'da_DK', 'de_DE', 'en_US', 'es_AR', 'es_ES', 'fr_FR', 'hi_IN', 'in_ID', 'it_IT', 'iw_IL', 'ja_JP', 'nl_NL', 'pl_PL', 'pt_BR', 'pt_PT', 'ro_RO', 'ru_RU', 'sv_SE', 'ta_IN', 'te_IN', 'tr_TR', 'zh_CN', 'zh_TW']; // app.use(`/desk/*`, (req, res) => {
    // const locale = 'ta_IN';

    const locale = locales[Math.floor(Math.random() * locales.length)];
    console.log({
      locale
    });
    return initalHTML.replaceAll(templateLabel, locale);
  }

  function replaceWithRandomDir(initalHTML, templateLabel) {
    const dirs = ['ltr', 'rtl']; // const dir = 'rtl';

    const dir = dirs[Math.floor(Math.random() * dirs.length)];
    console.log({
      dir
    });
    return initalHTML.replaceAll(templateLabel, dir);
  }
}