"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/from"));

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

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

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

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/definition"));

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

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

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

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Collection/functions"));

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

var tsplus_module_11 = /*#__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 all of the specified values to the queue, returning whether they
 * were offered to the queue.
 *
 * @tsplus fluent ets/TQueue offerAll
 */
function offerAll_(self, as0) {
  void 0;
  const as = tsplus_module_1.from(as0);
  return tsplus_module_11.effect((journal, fiberId) => {
    const queue = tsplus_module_2.unsafeGet_(self.ref, journal);

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

    if (tsplus_module_4.size(queue) + tsplus_module_5.length(as) <= self.capacity) {
      tsplus_module_7.unsafeSet_(self.ref, tsplus_module_6.appendAll_(queue, as), journal);
      return true;
    }

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

      case tsplus_module_8.Dropping:
        {
          const forQueue = tsplus_module_9.take_(as, self.capacity - tsplus_module_4.size(queue));
          tsplus_module_7.unsafeSet_(self.ref, tsplus_module_6.appendAll_(queue, forQueue), journal);
          return false;
        }

      case tsplus_module_8.Sliding:
        {
          const forQueue = tsplus_module_1.from(tsplus_module_9.take_(as, self.capacity));
          const toDrop = tsplus_module_4.size(queue) + tsplus_module_5.length(forQueue) - self.capacity;
          tsplus_module_7.unsafeSet_(self.ref, tsplus_module_6.appendAll_(tsplus_module_10.drop_(queue, toDrop), forQueue), journal);
          return true;
        }
    }

    return false;
  });
}
/**
 * Offers all of the specified values to the queue, returning whether they
 * were offered to the queue.
 *
 * @tsplus static ets/TQueue/Aspects offerAll
 */


const offerAll = as0 => self => offerAll_(self, as0);

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