import type { Middleware } from "@wrnexus/core";
import { authSession } from "../middleware.ts";
import {
  getDefaultAuthEngine,
  getDefaultAuthRouteOptions,
  hasDefaultAuthEngine,
} from "../runtime.ts";

/** Package middleware: hydrates auth state when a default engine is configured. */
const middleware: Middleware = async (ctx, next) => {
  if (!hasDefaultAuthEngine()) return next();
  return authSession(getDefaultAuthEngine(), {
    cookieName: getDefaultAuthRouteOptions().sessionCookieName,
  })(ctx, next);
};

export default middleware;
