import * as E from "../Either";
import { identity, pipe } from "../Function";
import * as schedule from "../Schedule";
import * as catchAll from "./catchAll";
import * as core from "./core";
import * as fail from "./fail";
import * as foldM from "./foldM";
import * as map from "./map";
import * as orDie from "./orDie";

function loop(self, orElse, driver) {
  return catchAll.catchAll_(map.map_(self, a => E.right(a)), e => foldM.foldM_(driver.next(e), () => core.chain_(orDie.orDie(driver.last), o => map.map_(orElse(e, o), a => E.left(a))), () => loop(self, orElse, driver)));
}
/**
 * Returns an effect that retries this effect with the specified schedule when it fails, until
 * the schedule is done, then both the value produced by the schedule together with the last
 * error are passed to the specified recovery function.
 */


export function retryOrElseEither_(self, policy, orElse, __trace) {
  return core.chain_(schedule.driver(policy), a => loop(self, orElse, a), __trace);
}
/**
 * Returns an effect that retries this effect with the specified schedule when it fails, until
 * the schedule is done, then both the value produced by the schedule together with the last
 * error are passed to the specified recovery function.
 *
 * @ets_data_first retryOrElseEither_
 */

export function retryOrElseEither(policy, orElse, __trace) {
  return self => retryOrElseEither_(self, policy, orElse, __trace);
}
/**
 * Retries with the specified schedule, until it fails, and then both the
 * value produced by the schedule together with the last error are passed to
 * the recovery function.
 */

export function retryOrElse_(self, policy, orElse, __trace) {
  return map.map_(retryOrElseEither_(self, policy, orElse, __trace), E.fold(identity, identity));
}
/**
 * Retries with the specified schedule, until it fails, and then both the
 * value produced by the schedule together with the last error are passed to
 * the recovery function.
 *
 * @ets_data_first retryOrElse_
 */

export function retryOrElse(policy, orElse, __trace) {
  return self => retryOrElse_(self, policy, orElse, __trace);
}
/**
 * Retries with the specified retry policy.
 * Retries are done following the failure of the original `io` (up to a fixed maximum with
 * `once` or `recurs` for example), so that that `io.retry(Schedule.once)` means
 * "execute `io` and in case of failure, try again once".
 */

export function retry_(self, policy, __trace) {
  return retryOrElse_(self, policy, (e, _) => fail.fail(e), __trace);
}
/**
 * Retries with the specified retry policy.
 * Retries are done following the failure of the original `io` (up to a fixed maximum with
 * `once` or `recurs` for example), so that that `io.retry(Schedule.once)` means
 * "execute `io` and in case of failure, try again once".
 *
 * @ets_data_first retry_
 */

export function retry(policy, __trace) {
  return self => retry_(self, policy, __trace);
}
//# sourceMappingURL=retry.js.map