"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.take = take;

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/isNonEmpty"));

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

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

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

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

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

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

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

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

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

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

var _InternalTPriorityQueue = /*#__PURE__*/require("@effect/core/stm/TPriorityQueue/operations/_internal/InternalTPriorityQueue");

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 a value from the queue, retrying until a value is in the queue.
 *
 * @tsplus getter ets/TPriorityQueue take
 */
function take(self) {
  return tsplus_module_11.effect(journal => {
    void 0;
    const map = tsplus_module_1.unsafeGet_(self.map, journal);
    const result = tsplus_module_5.flatMap_(tsplus_module_10.headMaybe(map), tuple => {
      const a = tsplus_module_5.flatMap_(tsplus_module_4.tail(tuple.get(1)), c => tsplus_module_3.fromPredicate(c, _ => tsplus_module_2.isNonEmpty(_)));
      const k = tuple.get(0);
      tsplus_module_8.unsafeSet_(self.map, a._tag === "None" ? tsplus_module_6.remove_(map, k) : tsplus_module_7.set_(map, k, a.value), journal);
      return tsplus_module_9.head(tuple.get(1));
    });

    if (result._tag === "None") {
      throw new _primitives.STMRetryException();
    }

    return result.value;
  });
}
//# sourceMappingURL=take.js.map