import * as tsplus_module_1 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeedNone";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/succeedSome";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Maybe/fold";
/**
 * Collects the first element of the `Collection<A?` for which the effectual
 * function `f` returns `Some`.
 *
 * @tsplus static ets/Effect/Ops collectFirst
 */

export function collectFirst(as, f, __tsplusTrace) {
  return tsplus_module_2.flatMap_(tsplus_module_1.succeed(as, __tsplusTrace), Collection => loop(Collection[Symbol.iterator](), f, __tsplusTrace), __tsplusTrace);
}

function loop(iterator, f, __tsplusTrace) {
  const next = iterator.next();
  return next.done ? tsplus_module_3.succeedNone : tsplus_module_2.flatMap_(f(next.value), option => tsplus_module_5.fold_(option, () => loop(iterator, f, __tsplusTrace), b => tsplus_module_4.succeedSome(() => b, __tsplusTrace)), __tsplusTrace);
}
//# sourceMappingURL=collectFirst.mjs.map