import { NextResponse } from "next/server"

import { findLang } from "@/constants/languages"
import { badRequest } from "@/server/errors/next"

import type { NextMiddleware } from "next/server"

import type { MiddlewareMatcher } from "@/types/middleware"

export const middleware: NextMiddleware = (request) => {
  const currentwebUrl = request.nextUrl.searchParams.get("currentweb")
  if (currentwebUrl == null || undefined) {
    return badRequest()
  }
  const lang = findLang(request.nextUrl.pathname)!

  return NextResponse.rewrite(new URL(`/${lang}/logout`, request.nextUrl))
}

export const matcher: MiddlewareMatcher = (request) => {
  return request.nextUrl.pathname.endsWith("/updatelogout")
}
