/** @type {import('next').NextConfig} */

const currentUrl = process.env.BASE_URL_BE || "";
const isProd = process.env.NODE_ENV === "production";
const baseUrl = `${currentUrl}/api/:path*`;
const imagebaseUrl = `${currentUrl}/:path*`;


const rewrites = () => {

  return [
    {
      source: '/api/:path*',
      destination: baseUrl
    },
    {
      source: '/images/:path*',
      destination: imagebaseUrl,
    },
  ];
};

const nextConfig = {
  rewrites,
  env: {
    NEXT_BACKEND: process.env.NEXT_BACKEND,
  },
  images: {
    remotePatterns: [
      {
        protocol: process.env.NODE_ENV === 'production' ? 'https' : 'http',
        hostname: process.env.NEXT_BACKEND || 'localhost',
        port: process.env.NODE_ENV === 'production' ? '' : '8000',
        pathname: 'public/upload/**',
      },
      {
        protocol: process.env.NODE_ENV === 'production' ? 'https' : 'http',
        hostname: process.env.NEXT_BACKEND || 'localhost',
        port: process.env.NODE_ENV === 'production' ? '' : '8000',
        pathname: 'public/static/**',
      },
      {
        hostname: 'images.pexels.com',
      },
      {
        hostname: 'cdn.pixabay.com',
      },
      {
        hostname: 'images.unsplash.com',
      },
    ],
  },
};

module.exports = nextConfig;
