import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import typescript from '@rollup/plugin-typescript';
import autoprefixer from 'autoprefixer';
import atImport from 'postcss-import';
import copy from 'rollup-plugin-copy';
import postcss from 'rollup-plugin-postcss';
import reactSvg from 'rollup-plugin-react-svg';
import versionInjector from 'rollup-plugin-version-injector';

const { NODE_ENV: targetEnv = 'development' } = process.env;
const outDirBase = '../html_ui';
const panelDirBase = `${outDirBase}/Pages/VCockpit/Instruments/aircraft_efb/KH_TFDi_MD11_efb`;

export default {
  input: 'src/App.tsx',
  output: {
    dir: panelDirBase,
    format: 'es',
    sourcemap: targetEnv !== 'production',
    manualChunks: (id) => {
      if (id.includes('node_modules')) {
        return 'vendor';
      }

      return null;
    },
  },
  plugins: [
    replace({
      'process.env.NODE_ENV': targetEnv,
      preventAssignment: true,
    }),
    versionInjector(),
    postcss({
      plugins: [autoprefixer(), atImport()],
      extract: true,
      use: ['sass'],
      sourceMap: 'inline',
      minimize: targetEnv === 'production',
    }),
    reactSvg(),
    resolve(),
    json(),
    typescript({
      outputToFilesystem: false,
    }),
    commonjs({
      requireReturnsDefault: 'auto',
    }),
    // targetEnv === 'production' && terser(), // Disabled due to script running afterwards
    copy({
      targets: [{ src: ['src/assets/*', '!**/*.svg'], dest: `${panelDirBase}/assets` }],
    }),
  ],
  onwarn: function (warning, warn) {
    if (warning.code === 'MODULE_LEVEL_DIRECTIVE') {
      return;
    }
    warn(warning);
  },
};
