import babel from "@rollup/plugin-babel"
import commonjs from "@rollup/plugin-commonjs"
import json from "@rollup/plugin-json"
import pkg from "./package.json"
import resolve from "@rollup/plugin-node-resolve"
import serve from "rollup-plugin-serve"
import license from "rollup-plugin-license"
import { terser } from "rollup-plugin-terser"

let { buildTarget } = process.env

if (typeof buildTarget === "undefined") {
  console.log('buildTarget undefined - setting to "DEV"')
  buildTarget = "DEV"
}

console.log(`buildTarget: ${buildTarget}`)

const env = {
  dev: buildTarget === "DEV",
  prod: buildTarget === "PROD",
}

const bannerText = `
<%= pkg.name %> v<%= pkg.version %>
<%= pkg.homepage %>
<%= pkg.description %>

Copyright 2013–<%= moment().format('YYYY') %>, <%= pkg.author.name %>
@license <%= pkg.license %>
`.trim()

const commonPreBabelOperations = useJson => [
  resolve(),
  useJson ? json() : undefined,
  commonjs(),
]

const commonPostBabelOperations = (isModule) => [
  env.prod &&
    terser({
      format: {
        comments: false,
      },
      compress: {
          unsafe: true, // Safe to turn on in most cases. This just means String(a) becomes "" + a
          unsafe_arrows: isModule, // Safe to turn on, unless there is an empty ES5 class declaration
          unsafe_math: true, // Safe to turn on, floating point math error is minor
      },
    }),
  license({
    sourcemap: true,
    banner: bannerText,
  }),
]

export default [
  {
    input: "browser.js",
    output: {
      file: pkg.main,
      format: "iife",
      name: "scratchblocks",
      sourcemap: env.prod,
    },
    plugins: [
      ...commonPreBabelOperations(),
      babel({ babelHelpers: "bundled" }),
      ...commonPostBabelOperations(),
      env.dev &&
        serve({
          contentBase: ".",
          port: 8000,
        }),
    ],
  },
  {
    input: "browser.es.js",
    output: {
      file: pkg.module,
      format: "esm",
      sourcemap: env.prod,
    },
    plugins: [
      ...commonPreBabelOperations(),
      // ESM bundle does not need Babel
      ...commonPostBabelOperations(true),
      env.dev &&
        serve({
          contentBase: ".",
          port: 8000,
        }),
    ],
  },
  {
    input: "locales-src/translations.js",
    output: {
      exports: "named",
      file: "build/translations.js",
      format: "iife",
      name: "translations",
      sourcemap: false,
    },
    plugins: [
      ...commonPreBabelOperations(true),
      babel({ babelHelpers: "bundled" }),
      ...commonPostBabelOperations(),
    ],
  },
  {
    input: "locales-src/translations-es.js",
    output: {
      file: "build/translations-es.js",
      format: "esm",
      sourcemap: false,
    },
    plugins: [
      ...commonPreBabelOperations(true),
      ...commonPostBabelOperations(true),
    ],
  },
  {
    input: "locales-src/translations-all.js",
    output: {
      file: "build/translations-all.js",
      format: "iife",
      name: "translationsAll",
      sourcemap: false,
    },
    plugins: [
      ...commonPreBabelOperations(true),
      babel({ babelHelpers: "bundled" }),
      ...commonPostBabelOperations(),
    ],
  },
  {
    input: "locales-src/translations-all-es.js",
    output: {
      file: "build/translations-all-es.js",
      format: "esm",
      sourcemap: false,
    },
    plugins: [
      ...commonPreBabelOperations(true),
      ...commonPostBabelOperations(true),
    ],
  },
]

@@ -972,17 +972,16 @@
 s = 
-(
 isModule
 ) =%3E
@@ -976,17 +976,16 @@
 isModule
-)
  =%3E %5B%0A  
@@ -1081,26 +1081,24 @@
 : %7B%0A        
-  
 unsafe: true
@@ -1174,18 +1174,16 @@
  %22%22 + a%0A
-  
         
@@ -1274,18 +1274,16 @@
 aration%0A
-  
         

Format file