// ets_tracing: off
import * as A from "../../Collections/Immutable/Chunk";
import { pipe } from "../../Function";
import * as O from "../../Option";
import * as T from "../_internal/effect";
import * as M from "../_internal/managed";
import * as Ref from "../_internal/ref";
import * as BP from "../BufferedPull";
import { Stream } from "./definitions";
/**
 * Statefully and effectfully maps over the elements of this stream to produce
 * new elements.
 *
 * @ets_data_first mapAccumM_
 */

export function mapAccumM(z, f) {
  return self => mapAccumM_(self, z, f);
}
/**
 * Statefully and effectfully maps over the elements of this stream to produce
 * new elements.
 */

export function mapAccumM_(self, z, f) {
  return new Stream(M.map_(M.bind_(M.bind_(M.do, "state", () => Ref.makeManagedRef(z)), "pull", () => M.mapM(BP.make)(self.proc)), ({
    pull,
    state
  }) => T.chain_(BP.pullElement(pull), o => T.mapError_(T.map_(T.tap_(T.bind_(T.bind_(T.do, "s", () => state.get), "t", ({
    s
  }) => f(s, o)), ({
    t
  }) => state.set(t.get(0))), ({
    t
  }) => A.single(t.get(1))), O.some))));
}
//# sourceMappingURL=mapAccumM.js.map