import { resolveRequestContext } from "@open-mercato/shared/lib/api/context";
import { hasFeature } from "@open-mercato/shared/security/features";
function hasOrganizationAccess(scopeOrganizationId, messageOrganizationId) {
  if (scopeOrganizationId) {
    return messageOrganizationId === scopeOrganizationId;
  }
  return messageOrganizationId == null;
}
async function resolveMessageContext(req) {
  const { ctx } = await resolveRequestContext(req);
  return {
    ctx,
    scope: {
      tenantId: ctx.auth?.tenantId ?? "",
      organizationId: ctx.auth?.orgId ?? null,
      userId: ctx.auth?.sub ?? ""
    }
  };
}
async function parseRequestBodySafe(req) {
  try {
    const text = await req.text();
    if (!text) return {};
    return JSON.parse(text);
  } catch {
    return {};
  }
}
async function canUseMessageEmailFeature(ctx, scope) {
  if (!scope.userId || !scope.tenantId) return false;
  const rbac = ctx.container.resolve("rbacService");
  const acl = await rbac.loadAcl(scope.userId, {
    tenantId: scope.tenantId,
    organizationId: scope.organizationId
  });
  return Boolean(acl.isSuperAdmin) || hasFeature(acl.features, "messages.email");
}
export {
  canUseMessageEmailFeature,
  hasOrganizationAccess,
  parseRequestBodySafe,
  resolveMessageContext
};
//# sourceMappingURL=routeHelpers.js.map
