import * as tracing_1 from "../../../../Tracing"
import * as tracing_2 from "../../../../Tracing"
const fileName_1 =
  "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/mapAccumM.ts"
import * as core from "../../../../Effect/core"
import * as coreMap from "../../../../Effect/map"
import * as Tp from "../../Tuple"
import * as Chunk from "../core"
import { concreteId } from "../definition"
/**
 * Statefully and effectfully maps over the elements of this chunk to produce
 * new elements.
 */

export function mapAccumM_(self, s, f) {
  return core.suspend(() => {
    const iterator = self.arrayLikeIterator()
    let dest = core.succeed(s, fileName_1 + ":19:45")
    let builder = Chunk.empty()
    let next

    while ((next = iterator.next()) && !next.done) {
      const array = next.value
      const length = array.length
      let i = 0

      while (i < length) {
        const a = array[i]
        dest = core.chain_(
          dest,
          (state) =>
            coreMap.map_(
              f(state, a),
              ({ tuple: [s, b] }) => {
                builder = Chunk.append_(builder, b)
                return s
              },
              fileName_1 + ":29:23"
            ),
          fileName_1 + ":28:27"
        )
        i++
      }
    }

    return coreMap.map_(dest, (s) => Tp.tuple(s, builder), fileName_1 + ":37:24")
  }, fileName_1 + ":17:22")
}
/**
 * Statefully and effectfully maps over the elements of this chunk to produce
 * new elements.
 *
 * @dataFirst mapAccumM_
 */

export function mapAccumM(s, f) {
  return (self) => mapAccumM_(self, s, f)
}
//# sourceMappingURL=mapAccumM.js.map
