"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/suspend"));

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

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/write"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/succeedNow"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zipRight"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fromEffect"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/flatMap"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fail"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/readWith"));

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

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

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

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

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

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

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

var _SinkInternal = /*#__PURE__*/require("@effect/core/stream/Sink/operations/_internal/SinkInternal");

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

/**
 * Creates a sink that effectfully folds elements of type `In` into a
 * structure of type `S`, until `max` worth of elements (determined by the
 * `costFn`) have been folded.
 *
 * The `decompose` function will be used for decomposing elements that cause
 * an `S` aggregate to cross `max` into smaller elements. Be vigilant with
 * this function, it has to generate "simpler" values or the fold may never
 * end. A value is considered indivisible if `decompose` yields the empty
 * chunk or a single-valued chunk. In these cases, there is no other choice
 * than to yield a value that will cross the threshold.
 *
 * @tsplus static ets/Sink/Ops foldWeightedDecomposeEffect
 */
function foldWeightedDecomposeEffect(z, costFn, max, decompose, f, __tsplusTrace) {
  return tsplus_module_1.suspend(() => new _SinkInternal.SinkInternal(go(z(), costFn, max, decompose, f, false, 0, __tsplusTrace)), __tsplusTrace);
}

function go(s, costFn, max, decompose, f, dirty, cost, __tsplusTrace) {
  return tsplus_module_9.readWith(chunk => tsplus_module_7.flatMap_(tsplus_module_6.fromEffect(() => fold(chunk, s, costFn, max, decompose, f, dirty, cost, 0, __tsplusTrace)), ({
    tuple: [nextS, nextCost, nextDirty, leftovers]
  }) => tsplus_module_2.isNonEmpty(leftovers) ? tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_4.succeedNow(nextS)) : cost > max ? tsplus_module_4.succeedNow(nextS) : go(nextS, costFn, max, decompose, f, nextDirty, nextCost, __tsplusTrace)), err => tsplus_module_8.fail(() => err), () => tsplus_module_4.succeedNow(s));
}

function fold(input, s, costFn, max, decompose, f, dirty, cost, index, __tsplusTrace) {
  if (index === input.length) {
    return tsplus_module_12.succeed(() => tsplus_module_11.make(s, cost, dirty, tsplus_module_10.empty()), __tsplusTrace);
  }

  const elem = tsplus_module_13.unsafeGet_(input, index);
  return tsplus_module_14.flatMap_(tsplus_module_16.map_(costFn(s, elem), addedCost => cost + addedCost, __tsplusTrace), total => {
    if (total <= max) {
      return tsplus_module_14.flatMap_(f(s, elem), s => fold(input, s, costFn, max, decompose, f, true, total, index + 1, __tsplusTrace), __tsplusTrace);
    }

    return tsplus_module_14.flatMap_(decompose(elem), decomposed => {
      if (decomposed.length <= 1 && !dirty) {
        // If `elem` cannot be decomposed, we need to cross the `max` threshold. To
        // minimize "injury", we only allow this when we haven't added anything else
        // to the aggregate (dirty = false).
        return tsplus_module_16.map_(f(s, elem), s => tsplus_module_11.make(s, total, true, tsplus_module_15.drop_(input, index + 1)), __tsplusTrace);
      }

      if (decomposed.length <= 1 && dirty) {
        // If the state is dirty and `elem` cannot be decomposed, we stop folding
        // and include `elem` in the leftovers.
        return tsplus_module_12.succeed(() => tsplus_module_11.make(s, cost, dirty, tsplus_module_15.drop_(input, index)), __tsplusTrace);
      } // `elem` got decomposed, so we will recurse with the decomposed elements pushed
      // into the chunk we're processing and see if we can aggregate further.


      return fold(tsplus_module_17.concat_(decomposed, tsplus_module_15.drop_(input, index + 1)), s, costFn, max, decompose, f, dirty, cost, 0, __tsplusTrace);
    }, __tsplusTrace);
  }, __tsplusTrace);
}
//# sourceMappingURL=foldWeightedDecomposeEffect.js.map