/* eslint-disable node/no-unpublished-import */
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import { terser } from 'rollup-plugin-terser'
import ts from '@rollup/plugin-typescript'
import babel from '@rollup/plugin-babel'
import builtins from 'builtin-modules'
//import sourcemaps from 'rollup-plugin-sourcemaps'
import jsonfile from '@rollup/plugin-json'
import alias from '@rollup/plugin-alias'
import { Plugin, RollupOptions, OutputOptions } from 'rollup'

if (!process.env.NODE_ENV || process.env.NODE_ENV == '') {
	throw Error('"process.env.NODE_ENV" needs to be set when bundling with Rollup.')
}

const sourcemap = process.env.NODE_ENV !== 'production' || process.env.SOURCEMAPS === 'true'

function configurePlugins(
	rootDir: string,
	typescript = true,
	production = process.env.NODE_ENV === 'production'
): Plugin[] {
	const common: Plugin[] = [
		resolve({
			rootDir,
			extensions: ['.mjs', '.js', '.json', '.jsx', '.node'],
		}),
		commonjs({
			sourceMap: sourcemap,
		}),
		jsonfile({}),
	]
	if (typescript) {
		common.push(ts())
	}
	if (production) {
		return [
			//sourcemaps(),
			alias({
				entries: [],
			}),
			...common,
			babel({
				babelHelpers: 'bundled',
				plugins: [
					'babel-plugin-inferno',
					[
						'transform-inline-environment-variables',
						{
							include: ['NODE_ENV'],
						},
					],
					'transform-remove-undefined',
					'transform-inline-consecutive-adds',
					'transform-property-literals',
					'transform-regexp-constructors',
					'minify-guarded-expressions',
					[
						'minify-dead-code-elimination',
						{
							keepFnName: true,
							/* eslint unicorn/prevent-abbreviations: "off" */
							keepFnArgs: true,
							keepClassName: true,
							tdz: true,
						},
					],
				],
				configFile: false,
				babelrc: false,
			}),
			terser({
				toplevel: true,
				ecma: 2020,
				parse: {
					shebang: true,
				},
				output: {
					comments: false,
					shebang: true,
					//preamble: formatLicense(),
				},
			}),
		]
	} else {
		return [
			//sourcemaps(),
			alias({
				entries: [
					{
						find: 'inferno',
						replacement: __dirname + '/node_modules/inferno/dist/index.dev.esm.js',
					},
				],
			}),
			...common,
			babel({
				babelHelpers: 'bundled',
				plugins: [
					'babel-plugin-inferno',
					['transform-inline-environment-variables', { include: ['NODE_ENV'] }],
					'transform-inline-consecutive-adds',
					[
						'minify-dead-code-elimination',
						{
							keepFnName: true,
							/* eslint unicorn/prevent-abbreviations: "off" */
							keepFnArgs: true,
							keepClassName: true,
							tdz: true,
						},
					],
				],
				configFile: false,
				babelrc: false,
			}),
			terser({
				ecma: 2020,
				keep_classnames: true,
				//keep_fnames: true,
				toplevel: true,
				parse: {
					shebang: true,
				},
				compress: {
					/* eslint unicorn/prevent-abbreviations: "off" */
					defaults: false,
					arrows: true,
					evaluate: true,
					properties: true,
					computed_props: true,
					dead_code: true,
				},
				output: {
					comments: 'some',
					shebang: true,
					beautify: true,
					semicolons: false,
					keep_quoted_props: true,
					indent_level: 2,
					max_line_len: 100,
				},
			}),
		]
	}
}

function outputBundle(filename: string): OutputOptions {
	return {
		file: filename,
		format: 'es',
		sourcemap,
	}
}

const bundles: RollupOptions[] = [
	{
		input: 'packages/basic/src/main.ts',
		external: builtins.concat('electron'),
		plugins: configurePlugins('packages/basic'),
		output: {
			...outputBundle('packages/dist/main.js'),
			format: 'cjs',
		},
	},
]

export default bundles