import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import injectProcessEnv from 'rollup-plugin-inject-process-env';
import json from '@rollup/plugin-json';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import nodeResolve from '@rollup/plugin-node-resolve';
import { terser } from "rollup-plugin-terser";
import typescript from '@rollup/plugin-typescript';
import fs from 'fs';

function googleAppsScript() {
  return {
    name: 'google-apps-script',
    renderChunk: (code, _chunk, options) => {
      const eval2 = eval;
      const provider = eval2(`${code}; ${options.name}.provider`);
      if (!provider) {
        console.warn('Warning! \'provider\' variable not exported!');

        return code;
      }

      const methods = {};
      provider.getExportedMethods().forEach((method) => {
        methods[method] = `function ${method}(...parameters) {\n  return ${options.name}.provider.callExportedMethod('${method}', parameters);\n}`;
      });

      const methodsNames = Object.keys(methods);
      methodsNames.sort();
      const methodsCode = methodsNames.map((method) => methods[method]).join('\n\n');

      return `${code}\n\n${methodsCode}`;
    }
  }
}

function getEnvironment() {
  var fileEnvironment = {};
  try {
    fileEnvironment = JSON.parse(fs.readFileSync(`${__dirname}/../_common/env.json`));
  } catch { }
  var processEnvironment = Object.fromEntries(Object.entries(process.env).filter(([key, _]) => key.startsWith('GAS_')).map(([key, value]) => [key.replace(/^GAS_/, ''), value]));

  return {
    ...fileEnvironment,
    ...processEnvironment
  };
}

export default {
  input: './src/index.ts',
  onwarn: function(warning, defaultHandler) {
    if (['THIS_IS_UNDEFINED'].includes(warning.code)) {
      return;
    }

    defaultHandler(warning);
  },
  output: {
    dir: './dist',
    format: 'iife',
    interop: 'auto',
    name: 'gas',
    validate: true
  },
  plugins: [
    typescript({
      tsconfig: './tsconfig.json'
    }),
    json({
      preferConst: true
    }),
    babel({
      babelrc: false,
      babelHelpers: 'bundled',
      generatorOpts: {
        comments: false
      },
      only: [
        './src'
      ],
      presets: [
        [
          '@babel/preset-env',
          {
            corejs: 3,
            modules: false,
            useBuiltIns: "entry",
            targets: {
              chrome: '55'
            },
          },
        ],
      ],
    }),
    nodeResolve({
      browser: true,
      dedupe: [
        'typedi'
      ],
      preferBuiltins: false
    }),
    commonjs({
      extensions: [
        '.js',
        '.ts'
      ],
      sourceMap: false,
      transformMixedEsModules: true
    }),
    nodePolyfills(),
    injectProcessEnv(getEnvironment()),
    googleAppsScript(),
    terser({
      ecma: 2019,
      keep_classnames: true
    })
  ],
  strictDeprecations: true
};
