/* eslint-disable  import/extensions */
/* eslint-disable @bigbinary/neeto/no-dangling-constants */

const getTranslationImports = require("../../scripts/buildTranslationImports.js");
const getEnglishTranslations = require("../../scripts/getEnglishTranslation.js");

const PREVAL_IMPORT_TRANSLATION_IMPORTS =
  'preval.require("../../configs/scripts/buildTranslationImports.js")';

const PREVAL_IMPORT_ENGLISH_TRANSLATIONS =
  'preval.require("../../configs/scripts/getEnglishTranslation.js")';

const prevalPlugin = () => ({
  name: "preval-plugin",
  transform(code, id) {
    if (
      id.includes("node_modules") &&
      (code.includes(PREVAL_IMPORT_TRANSLATION_IMPORTS) ||
        code.includes(PREVAL_IMPORT_ENGLISH_TRANSLATIONS))
    ) {
      const replacedCode = code
        .replace(PREVAL_IMPORT_TRANSLATION_IMPORTS, getTranslationImports())
        .replace(
          PREVAL_IMPORT_ENGLISH_TRANSLATIONS,
          JSON.stringify(getEnglishTranslations())
        );

      return { code: replacedCode, map: null };
    }

    return null;
  },
});

module.exports = { prevalPlugin };
