import * as tsplus_module_1 from "@effect/core/io/Effect/operations/fail";
import * as tsplus_module_2 from "@effect/core/stream/Stream/operations/tap";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/foldEffect";
import * as tsplus_module_5 from "@effect/core/stream/Stream/operations/unwrap";
import * as tsplus_module_6 from "@effect/core/stream/Stream/operations/catchAll";
import * as tsplus_module_7 from "@effect/core/io/Schedule/operations/driver";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/map";
/**
 * When the stream fails, retry it according to the given schedule
 *
 * This retries the entire stream, so will re-execute all of the stream's
 * acquire operations.
 *
 * The schedule is reset as soon as the first element passes through the
 * stream again.
 *
 * @param schedule Schedule receiving as input the errors of the stream.
 *
 * @tsplus fluent ets/Stream retry
 */

export function retry_(self, schedule, __tsplusTrace) {
  return tsplus_module_5.unwrap(() => tsplus_module_8.map_(tsplus_module_7.driver(schedule()), driver => {
    const loop = tsplus_module_6.catchAll_(self, e => tsplus_module_5.unwrap(() => tsplus_module_4.foldEffect_(driver.next(e), () => tsplus_module_1.fail(() => e, __tsplusTrace), () => tsplus_module_3.succeed(() => tsplus_module_2.tap_(loop, () => driver.reset, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace);
    return loop;
  }, __tsplusTrace), __tsplusTrace);
}
/**
 * When the stream fails, retry it according to the given schedule
 *
 * This retries the entire stream, so will re-execute all of the stream's
 * acquire operations.
 *
 * The schedule is reset as soon as the first element passes through the
 * stream again.
 *
 * @param schedule Schedule receiving as input the errors of the stream.
 *
 * @tsplus static ets/Stream/Aspects retry
 */

export const retry = (schedule, __tsplusTrace) => self => retry_(self, schedule, __tsplusTrace);
//# sourceMappingURL=retry.mjs.map