import * as tracing_1 from "../../../../Tracing"
import * as tracing_2 from "../../../../Tracing"
const fileName_1 =
  "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/collectM.ts"
import * as core from "../../../../Effect/core"
import * as map from "../../../../Effect/map"
import * as zipWith from "../../../../Effect/zipWith"
import * as O from "../../../../Option"
import * as Chunk from "../core"
import * as ChunkDef from "../definition"
/**
 * Returns a filtered, mapped subset of the elements of this chunk based on a .
 */

export function collectM_(self, f) {
  switch (self._typeId) {
    case ChunkDef.SingletonTypeId: {
      return O.fold_(
        f(self.a),
        () => core.succeed(Chunk.empty(), fileName_1 + ":22:27"),
        (b) => map.map_(b, Chunk.single, fileName_1 + ":23:24")
      )
    }

    case ChunkDef.ArrTypeId: {
      const array = self.arrayLike()
      let dest = core.succeed(Chunk.empty(), fileName_1 + ":28:60")

      for (let i = 0; i < array.length; i++) {
        const rhs = f(array[i])

        if (O.isSome(rhs)) {
          dest = zipWith.zipWith_(dest, rhs.value, Chunk.append_, fileName_1 + ":32:34")
        }
      }

      return dest
    }

    default: {
      return collectM_(self.materialize(), f)
    }
  }
}
/**
 * Returns a filtered, mapped subset of the elements of this chunk based on a .
 *
 * @dataFirst collectM_
 */

export function collectM(f) {
  return (self) => collectM_(self, f)
}
//# sourceMappingURL=collectM.js.map
