"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.retry = exports.retry_ = void 0;
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const SC = __importStar(require("../../../../Schedule"));
const CatchAll = __importStar(require("./catchAll"));
const Tap = __importStar(require("./tap"));
const Unwrap = __importStar(require("./unwrap"));
/**
 * 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.
 */
function retry_(self, schedule) {
    return Unwrap.unwrap((T.map_(T.bind_(T.do, "driver", () => SC.driver(schedule)), ({ driver }) => {
        const loop = (CatchAll.catchAll_(self, (e) => Unwrap.unwrap((T.foldM_(driver.next(e), (_) => T.fail(e), (_) => T.succeed(Tap.tap_(loop, (_) => driver.reset)))))));
        return loop;
    })));
}
exports.retry_ = retry_;
/**
 * 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.
 *
 * @ets_data_first retry_
 */
function retry(schedule) {
    return (self) => retry_(self, schedule);
}
exports.retry = retry;
//# sourceMappingURL=retry.js.map