"use strict";

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

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

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

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

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

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

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

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

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

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/repeatOrElseEither.ts";
const repeatOrElseEither_ = repeatOrElseEither_1;
/**
 * Returns a new effect that repeats this effect according to the specified
 * schedule or until the first failure, at which point, the failure value and
 * schedule output are passed to the specified handler.
 *
 * Scheduled recurrences are in addition to the first execution, so that
 * `io.repeat(Schedule.once)` yields an effect that executes `io`, and then if
 * that succeeds, executes `io` an additional time.
 *
 * @tsplus fluent ets/Effect repeatOrElseEither
 */

exports.repeatOrElseEither_ = repeatOrElseEither_;

function repeatOrElseEither_1(self, schedule, orElse, __tsplusTrace) {
  return tsplus_module_7.suspendSucceed(() => {
    const schedule0 = schedule();
    return tsplus_module_6.flatMap_(tsplus_module_5.driver(schedule0), driver => tsplus_module_4.foldEffect_(self, e => tsplus_module_3.map_(orElse(e, tsplus_module_2.none), tsplus_module_1.left, __tsplusTrace), a => repeatOrElseEitherLoop(self, driver, orElse, a), __tsplusTrace), __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * Returns a new effect that repeats this effect according to the specified
 * schedule or until the first failure, at which point, the failure value and
 * schedule output are passed to the specified handler.
 *
 * Scheduled recurrences are in addition to the first execution, so that
 * `io.repeat(Schedule.once)` yields an effect that executes `io`, and then if
 * that succeeds, executes `io` an additional time.
 *
 * @tsplus static ets/Effect/Aspects repeateOrElseEither
 */


function repeatOrElseEither(schedule, orElse, __tsplusTrace) {
  return self => repeatOrElseEither_1(self, schedule, orElse, __tsplusTrace);
}

function repeatOrElseEitherLoop(self, driver, orElse, value) {
  return tsplus_module_4.foldEffect_(driver.next(value), () => tsplus_module_3.map_(tsplus_module_8.orDie(driver.last, fileName_1 + ":57:28"), tsplus_module_1.right, fileName_1 + ":57:34"), b => tsplus_module_4.foldEffect_(self, e => tsplus_module_3.map_(orElse(e, tsplus_module_2.some(b)), tsplus_module_1.left, fileName_1 + ":60:44"), a => repeatOrElseEitherLoop(self, driver, orElse, a), fileName_1 + ":59:22"), fileName_1 + ":56:39");
}
//# sourceMappingURL=repeatOrElseEither.js.map