/// <summary>
        ///   Attempts to add an event to the batch, ensuring that the size
        ///   of the batch does not exceed its maximum.
        /// </summary>
        ///
        /// <param name="eventData">The event to attempt to add to the batch.</param>
        ///
        /// <returns><c>true</c> if the event was added; otherwise, <c>false</c>.</returns>
        ///
        public override bool TryAdd(EventData eventData)
        {
            Argument.AssertNotNull(eventData, nameof(eventData));
            Argument.AssertNotDisposed(_disposed, nameof(EventDataBatch));

            // Reserve space for producer-owned fields that correspond to special
            // features, if enabled.

            if ((ActiveFeatures & TransportProducerFeatures.IdempotentPublishing) != 0)
            {
                eventData.PendingPublishSequenceNumber = int.MaxValue;
                eventData.PendingProducerGroupId       = long.MaxValue;
                eventData.PendingProducerOwnerLevel    = short.MaxValue;
            }

            try
            {
                using var eventMessage = MessageConverter.CreateMessageFromEvent(eventData, Options.PartitionKey);

                // Calculate the size for the event, based on the AMQP message size and accounting for a
                // bit of reserved overhead size.

                var size = _sizeBytes
                           + eventMessage.SerializedMessageSize
                           + (eventMessage.SerializedMessageSize <= MaximumBytesSmallMessage
                        ? OverheadBytesSmallMessage
                        : OverheadBytesLargeMessage);

                if (size > MaximumSizeInBytes)
                {
                    return(false);
                }

                _sizeBytes = size;
                BatchEvents.Add(eventData);

                return(true);
            }
            finally
            {
                eventData.ClearPublishingState();
            }
        }