"use strict";

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

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

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

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

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

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

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

var tsplus_module_12 = /*#__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 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.
 *
 * The `foldWeightedDecomposeEffect` allows the decompose function to return a
 * `Effect` value, and consequently it allows the sink to fail.
 *
 * @tsplus static ets/Sink/Ops foldWeightedDecompose
 */
function foldWeightedDecompose(z, costFn, max, decompose, f, __tsplusTrace) {
  return tsplus_module_1.suspend(() => new _SinkInternal.SinkInternal(go(z(), costFn, decompose, f, false, 0, max, __tsplusTrace)), __tsplusTrace);
}

function go(s, costFn, decompose, f, dirty, cost, max, __tsplusTrace) {
  return tsplus_module_7.readWith(chunk => {
    const {
      tuple: [nextS, nextCost, nextDirty, leftovers]
    } = fold(chunk, s, costFn, decompose, f, dirty, cost, max, 0, __tsplusTrace);

    if (tsplus_module_2.isNonEmpty(leftovers)) {
      return tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_4.succeedNow(nextS));
    }

    if (cost > max) {
      return tsplus_module_4.succeedNow(nextS);
    }

    return go(nextS, costFn, decompose, f, nextDirty, nextCost, max, __tsplusTrace);
  }, err => tsplus_module_6.fail(() => err), () => tsplus_module_4.succeedNow(s));
}

function fold(input, s, costFn, decompose, f, dirty, cost, max, index, __tsplusTrace) {
  if (index === input.length) {
    return tsplus_module_9.make(s, cost, dirty, tsplus_module_8.empty());
  }

  const elem = tsplus_module_10.unsafeGet_(input, index);
  const total = cost + costFn(s, elem);

  if (total <= max) {
    return fold(input, f(s, elem), costFn, decompose, f, true, total, max, index + 1, __tsplusTrace);
  }

  const decomposed = decompose(elem);

  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_9.make(f(s, elem), total, true, tsplus_module_11.drop_(input, index + 1));
  }

  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_9.make(s, cost, dirty, tsplus_module_11.drop_(input, index));
  } // `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_12.concat_(decomposed, tsplus_module_11.drop_(input, index + 1)), s, costFn, decompose, f, dirty, cost, max, 0, __tsplusTrace);
}
//# sourceMappingURL=foldWeightedDecompose.js.map