"use strict";

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

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

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

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

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

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

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

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

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

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

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

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

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

var _StreamInternal = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/StreamInternal");

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

const fileName_1 = "(@effect/core) _src/stream/Stream/operations/rechunk.ts";

/**
 * Re-chunks the elements of the stream into chunks of `n` elements each. The
 * last chunk might contain less than `n` elements.
 *
 * @tsplus fluent ets/Stream rechunk
 */
function rechunk_(self, n, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_1.pipeTo_(self.channel, () => process(new Rechunker(n), n)));
}
/**
 * Re-chunks the elements of the stream into chunks of `n` elements each. The
 * last chunk might contain less than `n` elements.
 *
 * @tsplus static ets/Stream/Aspects rechunk
 */


const rechunk = (n, __tsplusTrace) => self => rechunk_(self, n, __tsplusTrace);

exports.rechunk = rechunk;

function process(rechunker, target) {
  return tsplus_module_11.readWithCause(chunk => {
    if (tsplus_module_2.size(chunk) === target && rechunker.isEmpty()) {
      return tsplus_module_4.zipRight_(tsplus_module_3.write(() => chunk), () => process(rechunker, target));
    }

    if (tsplus_module_2.size(chunk) > 0) {
      let chunks = tsplus_module_5.empty();
      let result = undefined;
      let i = 0;

      while (i < tsplus_module_2.size(chunk)) {
        while (i < tsplus_module_2.size(chunk) && result == null) {
          result = rechunker.write(tsplus_module_6.unsafeGet_(chunk, i));
          i = i + 1;
        }

        if (result != null) {
          chunks = tsplus_module_7.prepend_(chunks, result);
          result = undefined;
        }
      }

      return tsplus_module_4.zipRight_(tsplus_module_9.writeAll(...tsplus_module_8.reverse(chunks)), () => process(rechunker, target));
    }

    return process(rechunker, target);
  }, cause => tsplus_module_4.zipRight_(rechunker.emitIfNotEmpty(fileName_1 + ":58:40"), () => tsplus_module_10.failCause(() => cause)), () => rechunker.emitIfNotEmpty(fileName_1 + ":59:35"));
}

class Rechunker {
  constructor(n) {
    this.n = n;
    this.builder = tsplus_module_12.builder();
    this.pos = 0;
  }

  isEmpty() {
    return this.pos === 0;
  }

  write(elem) {
    this.builder.append(elem);
    this.pos += 1;

    if (this.pos === this.n) {
      const result = this.builder.build();
      this.builder = tsplus_module_12.builder();
      this.pos = 0;
      return result;
    }

    return undefined;
  }

  emitIfNotEmpty(__tsplusTrace) {
    if (this.pos !== 0) {
      return tsplus_module_3.write(() => this.builder.build());
    } else {
      return tsplus_module_13.unit;
    }
  }

}
//# sourceMappingURL=rechunk.js.map