import { NextResponse, type NextRequest } from "next/server";

/**
 * Marks embedded Shopify admin loads. Shopify opens the app with a `host` (and
 * `embedded`) query param; we surface that as a request header so the root
 * layout can inject App Bridge (as the first, non-async <script>) ONLY for the
 * embedded admin — never on the public site or affiliate portal.
 */
export function middleware(req: NextRequest) {
  const headers = new Headers(req.headers);
  const embedded =
    req.nextUrl.searchParams.has("host") || req.nextUrl.searchParams.has("embedded");
  headers.set("x-embedded", embedded ? "1" : "0");
  return NextResponse.next({ request: { headers } });
}

export const config = {
  matcher: ["/admin", "/admin/:path*"],
};
