import * as core from "../../../../Effect/core"
import type { Effect } from "../../../../Effect/effect"
import type * as Chunk from "../core"
import { concrete, SingletonTypeId } from "../definition"
import { reduceRight_ } from "./reduceRight"

/**
 * Folds over the elements in this chunk from the right.
 */
export function reduceRightM_<A, R, E, S>(
  self: Chunk.Chunk<A>,
  s: S,
  f: (a: A, s: S) => Effect<R, E, S>
): Effect<R, E, S> {
  concrete(self)
  if (self._typeId === SingletonTypeId) {
    return f(self.a, s)
  }
  return reduceRight_(self, core.succeed(s) as Effect<R, E, S>, (a, s) =>
    core.chain_(s, (s1) => f(a, s1))
  )
}

/**
 * Folds over the elements in this chunk from the right.
 *
 * @dataFirst reduceRightM_
 */
export function reduceRightM<A, R, E, S>(
  s: S,
  f: (a: A, s: S) => Effect<R, E, S>
): (self: Chunk.Chunk<A>) => Effect<R, E, S> {
  return (self) => reduceRightM_(self, s, f)
}

{'avg_line_length': 26.457142857142856, 'max_line_length': 73, 'alphanum_fraction': 0.5982721382289417, 'licenses': ['MIT'], 'repository_name': 'ahrjarrett/effect-ts-core', 'path': 'packages/system/src/Collections/Immutable/Chunk/api/reduceRightM.ts', 'size': 926, 'lang': 'TypeScript'}