import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { resolveFeatureCheckContext } from "@open-mercato/core/modules/directory/utils/organizationScope";
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
async function buildContext(req) {
  const container = await createRequestContainer();
  const auth = await getAuthFromRequest(req);
  if (!auth) throw new CrudHttpError(401, { error: "Unauthorized" });
  const { organizationId, scope } = await resolveFeatureCheckContext({ container, auth, request: req });
  const ctx = {
    container,
    auth,
    organizationScope: scope,
    selectedOrganizationId: organizationId ?? null,
    organizationIds: scope.allowedIds ?? (auth.orgId ? [auth.orgId] : null),
    request: req
  };
  return { ctx, auth, organizationId: organizationId ?? null, scope };
}
export {
  buildContext
};
//# sourceMappingURL=utils.js.map
