import type { OutputOptions, RollupOptions } from 'rollup'
import { packages } from './utils/packages'
import esbuild from 'rollup-plugin-esbuild'
import filesize from 'rollup-plugin-filesize'
import json from '@rollup/plugin-json'
import dts from 'rollup-plugin-dts'
import { getBabelOutputPlugin } from '@rollup/plugin-babel'
import { resolve } from 'path'

const configs: RollupOptions[] = []

const buildPlugins = [esbuild(), json(), filesize()]

const dtsPlugin = [dts()]

const externals = ['vue']

for (const { name, external, ignore } of packages) {
  if (ignore) {
    continue
  }
  const input = `packages/${name}/index.ts`

  const output: OutputOptions[] = [
    {
      format: 'cjs',
      file: `packages/${name}/dist/index.js`,
      plugins: [getBabelOutputPlugin({ configFile: resolve(__dirname, '../babel.config.js') })],
    },
    { format: 'esm', file: `packages/${name}/dist/index.mjs` },
  ]

  configs.push({
    input,
    output,
    plugins: buildPlugins,
    external: [...externals, ...(external || [])],
  })

  configs.push({
    input,
    output: {
      file: `packages/${name}/dist/index.d.ts`,
      format: 'es',
    },
    plugins: dtsPlugin,
    external: [...externals, ...(external || [])],
  })
}

export default configs
