import { terser } from 'rollup-plugin-terser';
import commonjs from 'rollup-plugin-commonjs';
import copy from 'rollup-plugin-copy';
import image from 'rollup-plugin-img';
import json from 'rollup-plugin-json';
import nodeResolve from 'rollup-plugin-node-resolve';
import pkg from './package.json';
import replace from 'rollup-plugin-replace';
import sourceMaps from 'rollup-plugin-sourcemaps';
import ttypescript from 'ttypescript';
import typescript from '@rollup/plugin-typescript';

/**
 * NODE_ENV explicit replacement is only needed for standalone packages, as webpack
 * automatically will replace it otherwise in the downstream build.
 */

const cjs = {
  exports: 'named',
  format: 'cjs',
  sourcemap: true,
};

const esm = {
  format: 'esm',
  sourcemap: true,
};

const getCJS = override => ({ ...cjs, ...override });
const getESM = override => ({ ...esm, ...override });

const commonPlugins = [
  copy({
    targets: [
      {
        src: ['./src/assets'],
        dest: ['dist/', 'native/dist/'] 
      },
    ]
  }),
  image({
    output: `dist/assets/images`,
    extensions: /\.(png|jpg|jpeg|gif|svg)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
    limit: 9000000,
  }),
  image({
    output: `native/dist/assets/images`,
    extensions: /\.(png|jpg|jpeg|gif|svg)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
    limit: 9000000,
  }),
  typescript({
    typescript: ttypescript,
    outputToFilesystem: true,
    tsconfig: './tsconfig.json',
  }),
  sourceMaps(),
  json(),
  nodeResolve(),
  commonjs({
    ignoreGlobal: true,
    include: /\/node_modules\//,
    namedExports: {
      react: Object.keys(require('react')),
      'react-is': Object.keys(require('react-is')),
      'react-native': ['View', 'Dimensions', 'TouchableOpacity', 'StyleSheet', 'Text'],
    },
  }),
  replace({
    __VERSION__: JSON.stringify(pkg.version),
  }),
];

// this should always be last
const minifierPlugin = terser({
  compress: {
    passes: 2,
  },
});

const configBase = {
  input: './src/index.ts',

  // \0 is rollup convention for generated in memory modules
  external: id => !id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'),
  plugins: commonPlugins,
};

const globals = { react: 'React', 'react-dom': 'ReactDOM', 'react-native': 'reactNative' };

const standaloneBaseConfig = {
  ...configBase,
  output: {
    file: 'dist/rewards-design-system.js',
    format: 'umd',
    globals,
    name: 'rds',
    sourcemap: true,
  },
  external: Object.keys(globals),
  plugins: configBase.plugins.concat(
    replace({
      __SERVER__: JSON.stringify(false),
    })
  ),
};

const standaloneConfig = {
  ...standaloneBaseConfig,
  plugins: standaloneBaseConfig.plugins.concat(
    replace({
      'process.env.NODE_ENV': JSON.stringify('development'),
    }),
    minifierPlugin
  ),
};

const standaloneProdConfig = {
  ...standaloneBaseConfig,
  output: {
    ...standaloneBaseConfig.output,
    file: 'dist/rewards-design-system.min.js',
  },
  plugins: standaloneBaseConfig.plugins.concat(
    replace({
      'process.env.NODE_ENV': JSON.stringify('production'),
    }),
    minifierPlugin
  ),
};

const serverConfig = {
  ...configBase,
  output: [
    getESM({ file: 'dist/rewards-design-system.esm.js' }),
    getCJS({ file: 'dist/rewards-design-system.cjs.js' }),
  ],
  plugins: configBase.plugins.concat(
    replace({
      window: global.window,
      __SERVER__: JSON.stringify(true),
    }),
    minifierPlugin
  ),
};

const browserConfig = {
  ...configBase,
  output: [
    getESM({ file: 'dist/rewards-design-system.browser.esm.js' }),
    getCJS({ file: 'dist/rewards-design-system.browser.cjs.js' }),
  ],
  plugins: configBase.plugins.concat(
    replace({
      __SERVER__: JSON.stringify(false),
    }),
    minifierPlugin
  ),
};

const nativeConfig = {
  ...configBase,
  input: './src/native/components/index.ts',
  output: [
    getCJS({
      file: 'native/dist/rewards-design-system.native.cjs.js',
    }),
    getESM({
      file: 'native/dist/rewards-design-system.native.esm.js',
    }),
  ],
};

export default [
  standaloneConfig,
  standaloneProdConfig,
  serverConfig,
  nativeConfig,
  browserConfig,
];