const fileName_1 = "(@effect/core) _src/stream/Stream/operations/branchAfter.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/suspend";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/concat";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/splitAt";
import * as tsplus_module_6 from "@effect/core/stream/Stream/operations/fromChunk";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_9 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_11 from "@effect/core/stream/Stream/operations/empty";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Reads the first `n` values from the stream and uses them to choose the
 * pipeline that will be used for the remainder of the stream.
 *
 * @tsplus fluent ets/Stream branchAfter
 */

export function branchAfter_(self, n, f, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_3.suspend(() => tsplus_module_2.pipeTo_(self.channel, () => collecting(tsplus_module_1.empty(), n, f, __tsplusTrace))));
}
/**
 * Reads the first `n` values from the stream and uses them to choose the
 * pipeline that will be used for the remainder of the stream.
 *
 * @tsplus static ets/Stream/Aspects branchAfter
 */

export const branchAfter = (n, f, __tsplusTrace) => self => branchAfter_(self, n, f, __tsplusTrace);

function collecting(buffer, n, f, __tsplusTrace) {
  return tsplus_module_12.readWithCause(chunk => {
    const newBuffer = tsplus_module_4.concat_(buffer, chunk);

    if (newBuffer.length >= n) {
      const {
        tuple: [inputs, inputs1]
      } = tsplus_module_5.splitAt_(newBuffer, n);
      const pipeline = f(inputs);
      const stream = pipeline(tsplus_module_6.fromChunk(() => inputs1, __tsplusTrace));
      void 0;
      return tsplus_module_7.zipRight_(stream.channel, () => emitting(pipeline));
    }

    return collecting(newBuffer, n, f, __tsplusTrace);
  }, cause => tsplus_module_8.failCause(() => cause), () => {
    if (tsplus_module_9.isEmpty(buffer)) {
      return tsplus_module_10.unit;
    }

    const pipeline = f(buffer);
    const stream = pipeline(tsplus_module_11.empty);
    void 0;
    return stream.channel;
  });
}

function emitting(pipeline) {
  return tsplus_module_12.readWithCause(chunk => {
    const stream = pipeline(tsplus_module_6.fromChunk(() => chunk, fileName_1 + ":71:47"));
    void 0;
    return tsplus_module_7.zipRight_(stream.channel, () => emitting(pipeline));
  }, cause => tsplus_module_8.failCause(() => cause), () => tsplus_module_10.unit);
}
//# sourceMappingURL=branchAfter.mjs.map