import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import typescript from 'rollup-plugin-typescript2';
import terser from '@rollup/plugin-terser';
import sizes from 'rollup-plugin-sizes';

/**
 * @type {import('rollup').RollupOptions}
 */
const browserBundles = [
  {
    input: 'src/client/index.ts',
    output: [
      {
        name: 'vonageClientSDK',
        file: 'dist/vonageClientSDK.js',
        format: 'umd',
        exports: 'named'
      },
      {
        name: 'vonageClientSDK',
        file: 'dist/vonageClientSDK.min.js',
        format: 'umd',
        plugins: [terser()],
        exports: 'named'
      },
      {
        file: 'dist/vonageClientSDK.mjs',
        format: 'es'
      },
      {
        file: 'dist/vonageClientSDK.min.mjs',
        format: 'es',
        plugins: [terser()]
      }
    ],
    treeshake: true,
    plugins: [
      json(),
      nodeResolve({
        jsnext: true,
        preferBuiltins: false,
        browser: true
      }),
      commonjs(),
      typescript({ include: ['*.{ts,tsx}', '**/*.{ts,tsx}'] })
    ]
  }
];

/**
 * @type {import('rollup').RollupOptions}
 */
const npmBundles = [
  {
    input: 'src/client/index.ts',
    external: [
      'socket.io-client',
      'socketio-wildcard',
      'sdp-transform',
      'events',
      'webrtc-adapter',
      '@js-joda/timezone'
    ],
    treeshake: false,
    output: [
      {
        file: './dist/client/index.mjs',
        format: 'es',
        exports: 'named'
      },
      {
        file: './dist/client/index.cjs',
        format: 'cjs',
        exports: 'named'
      }
    ],
    plugins: [
      json(),
      nodeResolve({ preferBuiltins: false }),
      commonjs(),
      typescript({
        include: ['*.{ts,tsx}', '**/*.{ts,tsx}'],
        tsconfigOverride: {
          compilerOptions: {
            declaration: false
          }
        }
      }),
      sizes()
    ]
  }
];

/**
 * @type {import('rollup').RollupOptions}
 */
export default [...browserBundles, ...npmBundles];
