// export { auth as middleware } from './auth'

// export const config = {
//   matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)']
// }

import { i18nRouter } from 'next-i18n-router';
import i18nConfig from './i18nConfig';
import { NextResponse, NextRequest, NextFetchEvent } from 'next/server'

import { authConfig } from './auth.config'
import NextAuth from 'next-auth';

export default NextAuth(authConfig).auth

export function isRouter(pathname: string) {
  return [
    "/xauth/login/",
    "/xauth/signup/",
    "/preview"
    // "/new/",
    // "/manage/",
  ].includes(pathname)
}

// This function can be marked `async` if using `await` inside
export function middleware(req: NextRequest, evt: NextFetchEvent) {
  // return NextResponse.redirect(new URL('/home', request.url))


  const foundItem: string | undefined = [
    "/xauth/login/",
    "/xauth/signup/",
    "/staffai"
    // "/new/",
    // "/manage/",
  ].find(item =>
    // item.includes(req.nextUrl.pathname)
    req.nextUrl.pathname.indexOf(item) !== -1
  )


  console.log(`
  ======> 
  ${req.url} - ${JSON.stringify(req)} - ${req.nextUrl.pathname} - ${foundItem}

 
  `)

  if (foundItem) {
    return
  }

  if (req.nextUrl.pathname.indexOf("manage") !== -1) {

    // 是否存在token

    // console.log(req.headers)
    console.log(req.headers.get("Authorization"))
    // return NextResponse.redirect(new URL("/new", req.url));
  }

  return i18nRouter(req, i18nConfig);
}

// export const config = {
//   matcher: [
//     "/app",
//     "/app/(.*)",
//     "/auth/(.*)",
//     "/(api|trpc)(.*)",
//     "/((?!_next/static|_next/image|images|favicon.ico|$).*)",
//   ],
// };

// only applies this middleware to files in the app directory
export const config = {
  matcher: '/((?!api|static|.*\\..*|_next).*)'
};

// export const config = {
//   matcher: ['/((?!api|_next/static|_next/image|favicon.ico|favicon.png).*)']
// }