"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.retryOrElseEither = retryOrElseEither;
exports.retryOrElseEither_ = void 0;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Schedule/operations/driver"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/suspendSucceed"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/definition"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/orDie"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/foldEffect"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/catchAll"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/io/Effect/operations/retryOrElseEither.ts";
const retryOrElseEither_ = retryOrElseEither_1;
/**
 * 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.
 *
 * @tsplus fluent ets/Effect retryOrElseEither
 */

exports.retryOrElseEither_ = retryOrElseEither_;

function retryOrElseEither_1(self, policy, orElse, __tsplusTrace) {
  return tsplus_module_3.suspendSucceed(() => {
    const schedule = policy();
    return tsplus_module_2.flatMap_(tsplus_module_1.driver(schedule), driver => retryOrElseEitherLoop(self, driver, orElse), __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * 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.
 *
 * @tsplus static ets/Effect/Aspects retryOrElseEither
 */


function retryOrElseEither(policy, orElse, __tsplusTrace) {
  return self => retryOrElseEither_1(self, policy, orElse, __tsplusTrace);
}

function retryOrElseEitherLoop(self, driver, orElse) {
  return tsplus_module_8.catchAll_(tsplus_module_5.map_(self, tsplus_module_4.right, fileName_1 + ":47:18"), e => tsplus_module_7.foldEffect_(driver.next(e), () => tsplus_module_2.flatMap_(tsplus_module_6.orDie(driver.last, fileName_1 + ":49:30"), out => tsplus_module_5.map_(orElse(e, out), tsplus_module_4.left, fileName_1 + ":49:68"), fileName_1 + ":49:40"), () => retryOrElseEitherLoop(self, driver, orElse), fileName_1 + ":48:30"), fileName_1 + ":47:41");
}
//# sourceMappingURL=retryOrElseEither.js.map