import createMiddleware from "next-intl/middleware";
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import { locales } from "./i18n";

function stackMiddleware(functions: Function[], index = 0): Function {
    const current = functions[index];
    if (current) {
        const next = stackMiddleware(functions, index + 1);
        return current(next);
    }
    return () => NextResponse.next();
}

function localMiddleware() {
    return async (request: NextRequest, _next: NextFetchEvent) => {
        const handleI18nRouting = createMiddleware({
            locales: locales,
            defaultLocale: locales.at(2)!,
        });
        return handleI18nRouting(request);
    };
}

const whiteRouters = ["/"];
function authMiddleware(next: Function) {
    return async (request: NextRequest, res: NextResponse) => {
        return next(request, res);
    };
}
export default stackMiddleware([authMiddleware, localMiddleware]);
// 创建一个正则表达式对象
export const config = {
    // todo use this to match the path
    matcher: ["/", "/(zh|en|es|br)/:path*"],
};
