"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 Function_1 = require("../../Function")
const O = __importStar(require("../../Option"))
const SC = __importStar(require("../../Schedule"))
const T = __importStar(require("../_internal/effect"))
const M = __importStar(require("../_internal/managed"))
const Ref = __importStar(require("../_internal/ref"))
const Pull = __importStar(require("../Pull"))
const definitions_1 = require("./definitions")
/**
 * 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.
 *
 * @param schedule Schedule receiving as input the errors of the stream
 * @return Stream outputting elements of all attempts of the stream
 */
function retry_(self, schedule) {
  return new definitions_1.Stream(
    M.map_(
      M.let_(
        M.tap_(
          M.bind_(
            M.bind_(
              M.bind_(M.do, "driver", () => T.toManaged(SC.driver(schedule))),
              "currStream",
              () => T.toManaged(Ref.makeRef(Pull.end))
            ),
            "switchStream",
            () => M.switchable()
          ),
          ({ currStream, switchStream }) =>
            T.toManaged(T.chain_(switchStream(self.proc), currStream.set))
        ),
        "pull",
        ({ currStream, driver, switchStream }) => {
          const loop = T.catchSome_(
            T.flatten(currStream.get),
            O.fold(
              () => O.none,
              (e) =>
                O.some(
                  T.foldM_(
                    driver.next(e),
                    (_) => Pull.fail(e),
                    (_) =>
                      T.zipRight_(
                        T.chain_(switchStream(self.proc), currStream.set),
                        T.tap_(loop, (_) => driver.reset)
                      )
                  )
                )
            )
          )
          return loop
        }
      ),
      ({ pull }) => pull
    )
  )
}
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.
 *
 * @param schedule Schedule receiving as input the errors of the stream
 * @return Stream outputting elements of all attempts of the stream
 */
function retry(schedule) {
  return (self) => retry_(self, schedule)
}
exports.retry = retry
//# sourceMappingURL=retry.js.map
