const fileName_1 = "(@effect/core) _src/stream/Stream/operations/collectEffect.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/readWithCause";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_11 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/unwrap";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Performs an effectful filter and map in a single step.
 *
 * @tsplus fluent ets/Stream collectEffect
 */

export function collectEffect_(self, pf, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => loop(tsplus_module_1.empty()[Symbol.iterator](), pf)));
}
/**
 * Performs an effectful filter and map in a single step.
 *
 * @tsplus static ets/Stream/Aspects collectEffect
 */

export const collectEffect = (pf, __tsplusTrace) => self => collectEffect_(self, pf, __tsplusTrace);

function loop(chunkIterator, pf) {
  const next = chunkIterator.next();

  if (next.done) {
    return tsplus_module_5.readWithCause(elem => loop(elem[Symbol.iterator](), pf), err => tsplus_module_3.failCause(() => err), done => tsplus_module_4.succeed(() => done));
  } else {
    return tsplus_module_12.unwrap(() => tsplus_module_11.fold_(pf(next.value), () => tsplus_module_6.succeed(() => loop(chunkIterator, pf), fileName_1 + ":40:29"), effect => tsplus_module_10.map_(effect, a1 => tsplus_module_9.zipRight_(tsplus_module_8.write(() => tsplus_module_7.single(a1)), () => loop(chunkIterator, pf)), fileName_1 + ":42:21")));
  }
}
//# sourceMappingURL=collectEffect.mjs.map