import * as tracing_1 from "../../../../Tracing"
import * as tracing_2 from "../../../../Tracing"
const fileName_1 =
  "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/reduceRight.ts"
import { ArrTypeId, concrete, SingletonTypeId } from "../definition"
/**
 * Folds over the elements in this chunk from the right.
 */

export function reduceRight_(self, s, f) {
  switch (self._typeId) {
    case SingletonTypeId: {
      return f(self.a, s)
    }

    case ArrTypeId: {
      const arr = self.arrayLike()
      const len = arr.length
      let s1 = s
      let i = len - 1

      while (i >= 0) {
        s1 = f(arr[i], s1)
        i--
      }

      return s1
    }

    default: {
      const iterator = self.reverseArrayLikeIterator()
      let next
      let s1 = s

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

        while (i >= 0) {
          const a = array[i]
          s1 = f(a, s1)
          i--
        }
      }

      return s1
    }
  }
}
/**
 * Folds over the elements in this chunk from the right.
 *
 * @dataFirst reduceRight_
 */

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