"use strict";

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

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

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/builder"));

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

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/THub/operations/_internal/InternalNode"));

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/STM/operations/effect"));

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

var _InternalTDequeue = /*#__PURE__*/require("@effect/core/stm/THub/operations/_internal/InternalTDequeue");

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; }

/**
 * Takes up to the specified number of values from the queue.
 *
 * @tsplus fluent ets/THub/TDequeue takeUpTo
 */
function takeUpTo_(self, max) {
  void 0;
  return tsplus_module_6.effect((journal, fiberId) => {
    let currentSubscriberHead = tsplus_module_1.unsafeGet_(self.subscriberHead, journal);

    if (currentSubscriberHead == null) {
      throw new _STM.STMInterruptException(fiberId);
    }

    const builder = tsplus_module_2.builder();
    let n = 0;

    while (n != max) {
      const node = tsplus_module_1.unsafeGet_(currentSubscriberHead, journal);

      if (node == null) {
        n = max;
      } else {
        const head = node.head;
        const tail = node.tail;

        if (head != null) {
          const subscribers = node.subscribers;

          if (subscribers == 1) {
            const size = tsplus_module_1.unsafeGet_(self.hubSize, journal);
            const updatedNode = tsplus_module_4.make(undefined, 0, node.tail);
            tsplus_module_5.unsafeSet_(currentSubscriberHead, updatedNode, journal);
            tsplus_module_5.unsafeSet_(self.publisherHead, tail, journal);
            tsplus_module_5.unsafeSet_(self.hubSize, size - 1, journal);
          } else {
            const updatedNode = tsplus_module_4.make(node.head, subscribers - 1, node.tail);
            tsplus_module_5.unsafeSet_(currentSubscriberHead, updatedNode, journal);
          }

          builder.append(head);
          n += 1;
        }

        currentSubscriberHead = tail;
      }
    }

    tsplus_module_5.unsafeSet_(self.subscriberHead, currentSubscriberHead, journal);
    return builder.build();
  });
}
/**
 * Takes up to the specified number of values from the queue.
 *
 * @tsplus static ets/THub/TDequeue/Aspects takeUpTo
 */


const takeUpTo = max => self => takeUpTo_(self, max);

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