import * as tsplus_module_1 from "@effect/core/stm/TRef/operations/unsafeGet";
import * as tsplus_module_2 from "@tsplus/stdlib/structure/Equals";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/ImmutableQueue/size";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/ImmutableQueue/append";
import * as tsplus_module_5 from "@effect/core/stm/TRef/operations/unsafeSet";
import * as tsplus_module_6 from "@effect/core/stm/TQueue/definition";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/ImmutableQueue/dequeue";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_9 from "@effect/core/stm/STM/operations/effect";
import { STMInterruptException, STMRetryException } from "@effect/core/stm/STM";
import { concreteTQueue } from "@effect/core/stm/TQueue/operations/_internal/InternalTQueue";
/**
 * Offers a value to the queue, returning whether the value was offered to the
 * queue.
 *
 * @tsplus fluent ets/TQueue offer
 */

export function offer_(self, a) {
  void 0;
  return tsplus_module_9.effect((journal, fiberId) => {
    const queue = tsplus_module_1.unsafeGet_(self.ref, journal);

    if (tsplus_module_2.equals(queue, null)) {
      throw new STMInterruptException(fiberId);
    }

    if (tsplus_module_3.size(queue) < self.capacity) {
      tsplus_module_5.unsafeSet_(self.ref, tsplus_module_4.append_(queue, a), journal);
      return true;
    }

    switch (self.strategy) {
      case tsplus_module_6.BackPressure:
        throw new STMRetryException();

      case tsplus_module_6.Dropping:
        return false;

      case tsplus_module_6.Sliding:
        {
          const dequeue = tsplus_module_7.dequeue(queue);

          if (tsplus_module_8.isSome(dequeue)) {
            tsplus_module_5.unsafeSet_(self.ref, tsplus_module_4.append_(queue, a), journal);
          }

          return true;
        }
    }

    return false;
  });
}
/**
 * Offers a value to the queue, returning whether the value was offered to the
 * queue.
 *
 * @tsplus static ets/TQueue/Aspects offer
 */

export const offer = a => self => offer_(self, a);
//# sourceMappingURL=offer.mjs.map