import { defineConfig } from 'tsup';

export default defineConfig([
  // Main bundled files
  {
    entry: [
      'src/index.js',
      'src/features/auth/index.js',
      'src/features/auth/actions.js',
      'src/features/auth/pages.js',
      'src/features/auth/components/index.js',
      'src/features/admin/index.js',
      'src/features/admin/actions.js',
      'src/features/admin/pages.js',
      'src/features/admin/components/index.js',
      'src/core/users/index.js',
      'src/core/users/constants.js',
      'src/core/api/index.js',
      'src/core/api/route-handler.js',
      'src/core/cron/index.js',
      'src/core/database/index.js',
      'src/core/database/cli.js',
      'src/core/email/index.js',
      'src/core/email/templates/index.js',
      'src/core/storage/index.js',
      'src/core/toast/index.js',
      'src/core/themes/index.js',
      'src/features/provider/index.js',
      'src/shared/components/index.js',
      'src/shared/Icons.js',
      'src/shared/lib/metadata/index.js',
      'src/shared/lib/logger.js',
      'src/shared/lib/appConfig.js',
      'src/shared/lib/rateLimit.js',
    ],
    format: ['esm'],
    dts: false,
    splitting: false,
    sourcemap: false,
    clean: true,
    external: ['react', 'react-dom', 'next', 'pg', 'dotenv', 'dotenv/config', 'resend', '@react-email/components', 'node-cron', 'readline', 'crypto', 'url', 'fs', 'path', 'net', 'dns', 'tls', '@zen/core/users', '@zen/core/users/constants', '@zen/core/api', '@zen/core/cron', '@zen/core/database', '@zen/core/email', '@zen/core/email/templates', '@zen/core/storage', '@zen/core/toast', '@zen/core/features/auth', '@zen/core/features/auth/actions', '@zen/core/features/auth/components', '@zen/core/shared/components', '@zen/core/shared/icons', '@zen/core/shared/logger', '@zen/core/shared/config', '@zen/core/shared/rate-limit', '@zen/core/themes', '@aws-sdk/client-s3', '@aws-sdk/s3-request-presigner'],
    noExternal: [],
    bundle: true,
    banner: {
      js: ``,
    },
    esbuildOptions(options) {
      options.loader = {
        '.js': 'jsx',
        '.jsx': 'jsx',
      };
      options.jsx = 'automatic';
      options.platform = 'neutral';
      options.legalComments = 'inline';
    },
  },
  // Page wrappers and server-only files - NOT bundled to preserve boundaries and share instances
  {
    entry: [
      'src/features/auth/page.js',
      'src/features/admin/page.js',
      'src/features/admin/navigation.server.js',
      'src/features/admin/dashboard/registry.js',
      'src/features/admin/dashboard/serverRegistry.js',
      'src/features/auth/dashboard.server.js',
      'src/features/dashboard.server.js',
    ],
    format: ['esm'],
    dts: false,
    splitting: false,
    sourcemap: false,
    clean: false, // Don't clean, we already did in first config
    external: [
      'react',
      'react-dom',
      'next',
      '@zen/core',
      '@zen/core/features/auth/pages',
      '@zen/core/features/auth/actions',
      '@zen/core/features/admin',
      '@zen/core/features/admin/pages',
      '@zen/core/features/admin/actions',
      '@zen/core/features/admin/navigation',
      '@zen/core/toast',
    ],
    bundle: false, // Don't bundle these files
    esbuildOptions(options) {
      options.outbase = 'src';
      options.loader = {
        '.js': 'jsx',
        '.jsx': 'jsx',
      };
      options.jsx = 'automatic';
    },
  },
]);

