import * as tsplus_module_1 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/isNonEmpty";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/reduce";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/fold";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/suspendSucceed";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/unwrap";
import * as tsplus_module_14 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_15 from "@effect/core/stream/Channel/operations/readWith";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Statefully and effectfully maps over the elements of this stream to produce
 * new elements.
 *
 * @tsplus fluent ets/Stream mapAccumEffect
 */

export function mapAccumEffect_(self, s, f, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_1.pipeTo_(self.channel, () => accumulator(s, f, __tsplusTrace)));
}
/**
 * Statefully and effectfully maps over the elements of this stream to produce
 * new elements.
 *
 * @tsplus static ets/Stream/Aspects mapAccumEffect
 */

export const mapAccumEffect = (s, f, __tsplusTrace) => self => mapAccumEffect_(self, s, f, __tsplusTrace);

function accumulator(s, f, __tsplusTrace) {
  return tsplus_module_15.readWith(chunk => tsplus_module_13.unwrap(() => tsplus_module_12.suspendSucceed(() => {
    const outputChunk = tsplus_module_2.builder();

    const emit = out => tsplus_module_3.succeed(() => {
      outputChunk.append(out);
    }, __tsplusTrace);

    return tsplus_module_11.fold_(tsplus_module_10.reduce(() => chunk, s, (s, a) => tsplus_module_9.flatMap_(f(s, a), ({
      tuple: [s, a2]
    }) => tsplus_module_8.as_(emit(a2), () => s, __tsplusTrace), __tsplusTrace), __tsplusTrace), failure => {
      const partialResult = outputChunk.build();
      return tsplus_module_4.isNonEmpty(partialResult) ? tsplus_module_7.zipRight_(tsplus_module_5.write(() => partialResult), () => tsplus_module_6.fail(() => failure)) : tsplus_module_6.fail(() => failure);
    }, out => tsplus_module_7.zipRight_(tsplus_module_5.write(() => outputChunk.build()), () => accumulator(() => out, f, __tsplusTrace)), __tsplusTrace);
  }, __tsplusTrace)), err => tsplus_module_6.fail(() => err), () => tsplus_module_14.unit);
}
//# sourceMappingURL=mapAccumEffect.mjs.map