"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.offer = void 0;
exports.offer_ = offer_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TRef/operations/unsafeGet"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/structure/Equals"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/ImmutableQueue/size"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/ImmutableQueue/append"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TRef/operations/unsafeSet"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TQueue/definition"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/ImmutableQueue/dequeue"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/operations/effect"));

var _STM = /*#__PURE__*/require("@effect/core/stm/STM");

var _InternalTQueue = /*#__PURE__*/require("@effect/core/stm/TQueue/operations/_internal/InternalTQueue");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * Offers a value to the queue, returning whether the value was offered to the
 * queue.
 *
 * @tsplus fluent ets/TQueue offer
 */
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 _STM.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 _STM.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
 */


const offer = a => self => offer_(self, a);

exports.offer = offer;
//# sourceMappingURL=offer.js.map