import { NextResponse } from 'next/server';

export function stackMiddlewares(functions = []) {
  return async (request, event) => {
    let index = 0;

    const next = async (req, evt) => {
      const current = functions[index];
      index++;

      if (current) {
        return current(next)(req, evt);
      }

      return NextResponse.next();
    };

    return next(request, event);
  };
}
