import { getPublicNextURL } from "@/server/utils"

import { findLang } from "@/utils/languages"
import { removeTrailingSlash } from "@/utils/url"

import type { NextRequest } from "next/server"

export function getDefaultRequestHeaders(request: NextRequest) {
  const lang = findLang(request.nextUrl.pathname)!
  const nextUrlPublic = getPublicNextURL(request)
  const headers = new Headers(request.headers)
  headers.set("x-lang", lang)
  headers.set(
    "x-pathname",
    removeTrailingSlash(
      request.nextUrl.pathname.replace(`/${lang}`, "").replace(`/webview`, "")
    )
  )
  headers.set("x-url", removeTrailingSlash(nextUrlPublic.href))

  return headers
}
