import type { NextConfig } from 'next';

// Replit development adapter (approved in docs/replit-handoff/05):
// when API_PROXY_TARGET is set (Replit dev), Next.js proxies /api/v1/* same-origin
// to the internal NestJS server. Outside Replit this env var is unset and the
// original two-port arrangement (NEXT_PUBLIC_API_BASE_URL) continues to work.
const apiProxyTarget = process.env.API_PROXY_TARGET;
const allowedDevOrigins = [
  process.env.REPLIT_DEV_DOMAIN,
  'localhost',
  '127.0.0.1',
].filter((origin): origin is string => Boolean(origin));

const nextConfig: NextConfig = {
  reactStrictMode: true,
  transpilePackages: ['@ali-ismail/ui', '@ali-ismail/contracts'],
  allowedDevOrigins,
  ...(apiProxyTarget
    ? {
        async rewrites() {
          return [
            {
              source: '/api/v1/:path*',
              destination: `${apiProxyTarget}/api/v1/:path*`,
            },
          ];
        },
      }
    : {}),
};

export default nextConfig;
