const fileName_1 = "(@effect/core) _src/stream/Stream/operations/rechunk.ts";
import * as tsplus_module_1 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/size";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/List/prepend";
import * as tsplus_module_8 from "@tsplus/stdlib/collections/List/reverse";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/writeAll";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/readWithCause";
import * as tsplus_module_12 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/unit";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * 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
 */

export function rechunk_(self, n, __tsplusTrace) {
  void 0;
  return new 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
 */

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

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.mjs.map