import * as tsplus_module_1 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Maybe/map";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/mapAccum";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/collect";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_11 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_12 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/readWith";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Zips each element with the next element if present.
 *
 * @tsplus fluent ets/Stream zipWithNext
 */

export function zipWithNext(self, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => process(tsplus_module_1.none, __tsplusTrace)));
}

function process(last, __tsplusTrace) {
  return tsplus_module_13.readWith(input => {
    const {
      tuple: [newLast, chunk]
    } = tsplus_module_5.mapAccum_(input, last, (prev, curr) => tsplus_module_3.make(tsplus_module_1.some(curr), tsplus_module_4.map_(prev, a => tsplus_module_3.make(a, curr))));
    const out = tsplus_module_6.collect_(chunk, option => tsplus_module_1.isSome(option) ? tsplus_module_1.some(tsplus_module_3.make(option.value.get(0), tsplus_module_1.some(option.value.get(1)))) : tsplus_module_1.none);
    return tsplus_module_8.zipRight_(tsplus_module_7.write(() => out), () => process(newLast, __tsplusTrace));
  }, err => tsplus_module_9.fail(() => err), () => tsplus_module_12.fold_(last, () => tsplus_module_10.unit, a => tsplus_module_8.zipRight_(tsplus_module_7.write(() => tsplus_module_11.single(tsplus_module_3.make(a, tsplus_module_1.none))), () => tsplus_module_10.unit)));
}
//# sourceMappingURL=zipWithNext.mjs.map