import { AsyncLocalStorage } from 'node:async_hooks';

/**
 * Per-request shop context for the embedded multi-tenant app. A global
 * middleware verifies the Shopify session token and runs the rest of the
 * request inside a store carrying that shop domain — so any code (e.g.
 * getMerchant) can resolve the current merchant WITHOUT threading `shop`
 * through every call. Falls back to the demo shop when no token is present.
 */
export const shopContext = new AsyncLocalStorage<{ shop?: string }>();

/** The verified shop domain for the current request, if any. */
export function currentShop(): string | undefined {
  return shopContext.getStore()?.shop;
}
