import type { NextConfig } from "next";

const normalizeApiOrigin = (value?: string) => {
  if (!value) return "";
  return value.replace(/\/$/, "").replace(/\/api$/, "");
};

const apiProxyOrigin = normalizeApiOrigin(process.env.API_PROXY_TARGET);

const nextConfig: NextConfig = {
  async rewrites() {
    if (!apiProxyOrigin) {
      return [];
    }

    return [
      {
        source: "/api/:path*",
        destination: `${apiProxyOrigin}/api/:path*`,
      },
    ];
  },
};

export default nextConfig;
