import { NextResponse } from "next/server";
import {
  MIDDLEWARE_REGISTRY,
  MIDDLEWARE_METADATA
} from "@nextsparkjs/registries/middleware-registry";
class MiddlewareService {
  // ============== Lookup Methods ==============
  /**
   * Get middleware for specific theme
   * @complexity O(1)
   */
  static getByTheme(themeName) {
    return MIDDLEWARE_REGISTRY[themeName];
  }
  /**
   * Get all registered middlewares
   * @complexity O(n) where n = number of themes with middleware
   */
  static getAll() {
    return Object.values(MIDDLEWARE_REGISTRY);
  }
  /**
   * Check if theme has middleware
   * @complexity O(1)
   */
  static hasMiddleware(themeName) {
    return themeName in MIDDLEWARE_REGISTRY && MIDDLEWARE_REGISTRY[themeName].exists;
  }
  /**
   * Get registry metadata
   * @complexity O(1)
   */
  static getMetadata() {
    return MIDDLEWARE_METADATA;
  }
  // ============== Execution Methods ==============
  /**
   * Execute theme middleware with error handling
   * @complexity O(1) + async execution time
   */
  static async execute(themeName, request, coreSession) {
    const entry = MIDDLEWARE_REGISTRY[themeName];
    if (!entry || !entry.exists) {
      return null;
    }
    try {
      return await entry.middleware(request, coreSession);
    } catch (error) {
      console.error(`Error executing middleware for theme '${themeName}':`, error);
      return null;
    }
  }
  // ============== Helper Methods ==============
  /**
   * Redirect to login with callback URL
   * Use for unauthenticated users trying to access protected routes
   * @complexity O(1)
   */
  static redirectWithoutSession(request, targetPath = "/login") {
    const { pathname } = request.nextUrl;
    const loginUrl = new URL(targetPath, request.url);
    loginUrl.searchParams.set("callbackUrl", pathname);
    return NextResponse.redirect(loginUrl);
  }
  /**
   * Redirect authenticated user to target path
   * Use for authenticated users trying to access login/signup pages
   * @complexity O(1)
   */
  static redirectWithSession(request, targetPath = "/dashboard") {
    return NextResponse.redirect(new URL(targetPath, request.url));
  }
  /**
   * Add user headers to request for downstream processing
   * Adds x-user-id, x-user-email, and x-pathname headers
   * @complexity O(1)
   */
  static addUserHeaders(request, sessionUser) {
    const requestHeaders = new Headers(request.headers);
    if (sessionUser == null ? void 0 : sessionUser.id) {
      requestHeaders.set("x-user-id", sessionUser.id);
    }
    if (sessionUser == null ? void 0 : sessionUser.email) {
      requestHeaders.set("x-user-email", sessionUser.email);
    }
    requestHeaders.set("x-pathname", request.nextUrl.pathname);
    return NextResponse.next({
      request: {
        headers: requestHeaders
      }
    });
  }
}
const getActiveThemeMiddleware = MiddlewareService.getByTheme;
const getAllMiddlewares = MiddlewareService.getAll;
const hasThemeMiddleware = MiddlewareService.hasMiddleware;
const executeThemeMiddleware = MiddlewareService.execute;
const redirectWithoutSessionMiddleware = MiddlewareService.redirectWithoutSession;
const redirectWithSessionMiddleware = MiddlewareService.redirectWithSession;
const addUserHeadersMiddleware = MiddlewareService.addUserHeaders;
export {
  MiddlewareService,
  addUserHeadersMiddleware,
  executeThemeMiddleware,
  getActiveThemeMiddleware,
  getAllMiddlewares,
  hasThemeMiddleware,
  redirectWithSessionMiddleware,
  redirectWithoutSessionMiddleware
};
