"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.repeat = exports.repeat_ = exports.repeatOrElse = exports.repeatOrElse_ = exports.repeatOrElseEither = exports.repeatOrElseEither_ = void 0;
const E = __importStar(require("../Either"));
const Function_1 = require("../Function");
const O = __importStar(require("../Option"));
const S = __importStar(require("../Schedule"));
const core_1 = require("./core");
const fail_1 = require("./fail");
const foldM_1 = require("./foldM");
const map = __importStar(require("./map"));
const orDie_1 = require("./orDie");
/**
 * 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.
 */
function repeatOrElseEither_(self, schedule, orElse, __trace) {
    return ((0, core_1.chain)((driver) => {
        function loop(a) {
            return ((0, foldM_1.foldM)(() => (map.map_((0, orDie_1.orDie)(driver.last), E.right)), (b) => ((0, foldM_1.foldM)((e) => (map.map_(orElse(e, O.some(b)), E.left)), (a) => loop(a))(self)))(driver.next(a)));
        }
        return ((0, foldM_1.foldM)((e) => (map.map_(orElse(e, O.none), E.left)), (a) => loop(a), __trace)(self));
    })(S.driver(schedule)));
}
exports.repeatOrElseEither_ = repeatOrElseEither_;
/**
 * 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.
 *
 * @ets_data_first repeatOrElseEither_
 */
function repeatOrElseEither(schedule, orElse, __trace) {
    return (self) => repeatOrElseEither_(self, schedule, orElse, __trace);
}
exports.repeatOrElseEither = repeatOrElseEither;
/**
 * 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.
 */
function repeatOrElse_(self, schedule, orElse, __trace) {
    return map.map_(repeatOrElseEither_(self, schedule, orElse, __trace), E.merge);
}
exports.repeatOrElse_ = repeatOrElse_;
/**
 * 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.
 *
 * @ets_data_first repeatOrElse_
 */
function repeatOrElse(schedule, orElse, __trace) {
    return (self) => repeatOrElse_(self, schedule, orElse, __trace);
}
exports.repeatOrElse = repeatOrElse;
/**
 * Returns a new effect that repeats this effect according to the specified
 * schedule or until the first failure. 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.
 */
function repeat_(self, schedule, __trace) {
    return repeatOrElse_(self, schedule, (e) => (0, fail_1.fail)(e), __trace);
}
exports.repeat_ = repeat_;
/**
 * Returns a new effect that repeats this effect according to the specified
 * schedule or until the first failure. 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.
 *
 * @ets_data_first repeat_
 */
function repeat(schedule, __trace) {
    return (self) => repeat_(self, schedule, __trace);
}
exports.repeat = repeat;
//# sourceMappingURL=repeat.js.map