import { hasFeature } from "@open-mercato/shared/security/features";
const ADMIN_FEATURE = "communication_channels.admin";
function assertCanAccessChannel(channel, currentUserId, userFeatures) {
  if (!channel) {
    throw new ChannelAccessDeniedError("Channel not found");
  }
  void userFeatures;
  if (channel.userId == null) return;
  if (channel.userId !== currentUserId) {
    throw new ChannelAccessDeniedError(
      "Channel is a personal mailbox owned by another user"
    );
  }
}
function assertCanManageChannel(channel, currentUserId, userFeatures, elevatedFeature) {
  if (!channel) {
    throw new ChannelAccessDeniedError("Channel not found");
  }
  if (channel.userId != null) {
    if (channel.userId !== currentUserId) {
      throw new ChannelAccessDeniedError("Channel is a personal mailbox owned by another user");
    }
    return;
  }
  const grantedFeatures = Array.isArray(userFeatures) ? userFeatures : [];
  if (!hasFeature(grantedFeatures, elevatedFeature)) {
    throw new ChannelAccessDeniedError(`Managing a shared channel requires '${elevatedFeature}'`);
  }
}
class ChannelAccessDeniedError extends Error {
  constructor() {
    super(...arguments);
    this.name = "ChannelAccessDeniedError";
    this.statusCode = 403;
  }
}
export {
  ADMIN_FEATURE,
  ChannelAccessDeniedError,
  assertCanAccessChannel,
  assertCanManageChannel
};
//# sourceMappingURL=access-control.js.map
