import { withPayload } from "@payloadcms/next/withPayload";
import withMintelConfig from "@mintel/next-config";

/** @type {import('next').NextConfig} */
const nextConfig = {
    async rewrites() {
        const umamiUrl =
            process.env.UMAMI_API_ENDPOINT ||
            process.env.UMAMI_SCRIPT_URL ||
            process.env.NEXT_PUBLIC_UMAMI_SCRIPT_URL ||
            "https://analytics.infra.mintel.me";
        const glitchtipUrl = process.env.SENTRY_DSN
            ? new URL(process.env.SENTRY_DSN).origin
            : "https://errors.infra.mintel.me";

        return [
            {
                source: "/stats/:path*",
                destination: `${umamiUrl}/:path*`,
            },
            {
                source: "/:locale(de)/stats/:path*",
                destination: `${umamiUrl}/:path*`,
            },
            {
                source: "/errors/:path*",
                destination: `${glitchtipUrl}/:path*`,
            },
            {
                source: "/:locale(de)/errors/:path*",
                destination: `${glitchtipUrl}/:path*`,
            },
        ];
    },
};

export default withPayload(withMintelConfig(nextConfig));
