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

const getUncachedTranslations = () => {
  delete require.cache[require.resolve("../../scripts/getPkgTranslations.js")];

  return require("../../scripts/getPkgTranslations.js");
};

const PREVAL_IMPORT =
  'preval.require("../../configs/scripts/getPkgTranslations.js")';

const prevalPlugin = () => ({
  name: "preval-plugin",
  transform(code, id) {
    if (id.includes("node_modules") && code.includes(PREVAL_IMPORT)) {
      const replacedCode = code.replace(
        PREVAL_IMPORT,
        JSON.stringify(getUncachedTranslations())
      );

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

    return null;
  },
});

module.exports = { prevalPlugin };
