import { NextResponse } from "next/server";
import fs from "fs";
import { permanentRedirect, redirect } from "next/navigation";
// the following code is taken from : https://nextjs.org/docs/advanced-features/middleware#setting-headers
export async function middleware(request) {
  const firstPathname = request.nextUrl.pathname;

  const response = await fetch(
    `${process.env.NEXT_PUBLIC_API_URL}/page/redirect/check?oldUrl=${request.nextUrl.pathname}`
  );
  if (response.ok) {
    const newUrl = await response.text();
    return NextResponse.next({
      status: 301,
      headers: {
        Location: `${process.env.NEXT_PUBLIC_APP_URL}${newUrl}`,
      },
    });
  }
  return NextResponse.next();
}

// the following code has been copied from https://nextjs.org/docs/advanced-features/middleware#matcher
export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     */
    "/((?!api|_next/static|_next/image|favicon|fonts|favicon.ico).*)",
  ],
};

[];
