/* eslint-disable  import/extensions */
const PkgTranslations = require("@bigbinary/neeto-commons-frontend/configs/scripts/getPkgTranslations.js");

const prevalPlugin = () => ({
  name: "preval-plugin",
  setup(build) {
    build.onLoad(
      {
        filter:
          /node_modules\/@bigbinary\/neeto-commons-frontend\/dist\/(cjs\/)?initializers\/i18n.js/,
      },
      async args => {
        const fs = require("fs").promises;
        const contents = await fs.readFile(args.path, "utf8");

        const transformedContents = contents.replace(
          `preval.require("../../configs/scripts/getPkgTranslations.js")`,
          JSON.stringify(PkgTranslations)
        );

        return { contents: transformedContents, loader: "js" };
      }
    );
  },
});

module.exports = { prevalPlugin };
