"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.catchAllCause = exports.catchAllCause_ = void 0;
// ets_tracing: off
const C = __importStar(require("../../Cause"));
const Tp = __importStar(require("../../Collections/Immutable/Tuple"));
const Ex = __importStar(require("../../Exit"));
const Function_1 = require("../../Function");
const Finalizer = __importStar(require("../../Managed/ReleaseMap/finalizer"));
const makeReleaseMap = __importStar(require("../../Managed/ReleaseMap/makeReleaseMap"));
const releaseAll = __importStar(require("../../Managed/ReleaseMap/releaseAll"));
const Option = __importStar(require("../../Option"));
const T = __importStar(require("../_internal/effect"));
const M = __importStar(require("../_internal/managed"));
const Ref = __importStar(require("../_internal/ref"));
const definitions_1 = require("./definitions");
/**
 * Switches over to the stream produced by the provided function in case this one
 * fails. Allows recovery from all causes of failure, including interruption if the
 * stream is uninterruptible.
 */
function catchAllCause_(self, f) {
    return new definitions_1.Stream((M.map_(M.let_(M.bind_(M.bind_(M.do, "finalizerRef", () => M.finalizerRef(Finalizer.noopFinalizer)), "ref", () => (T.toManaged(Ref.makeRef({ _tag: "NotStarted" })))), "pull", ({ finalizerRef, ref }) => {
        const closeCurrent = (cause) => (T.uninterruptible(T.chain_(Ref.getAndSet_(finalizerRef, Finalizer.noopFinalizer), (f) => f(Ex.halt(cause)))));
        const open = (stream) => (asState) => T.uninterruptibleMask(({ restore }) => (T.chain_(makeReleaseMap.makeReleaseMap, (releaseMap) => (T.chain_(finalizerRef.set((exit) => releaseAll.releaseAll(exit, T.sequential)(releaseMap)), () => (T.tap_(T.map_(T.provideSome_(restore(stream.proc.effect), (_) => Tp.tuple(_, releaseMap)), ({ tuple: [_, __] }) => __), (pull) => ref.set(asState(pull)))))))));
        const failover = (cause) => (Option.fold_(C.sequenceCauseOption(cause), () => T.fail(Option.none), (cause) => (T.flatten(T.chain_(closeCurrent(cause), () => open(f(cause))((pull) => ({
            _tag: "Other",
            pull
        })))))));
        return (T.chain_(ref.get, (s) => {
            switch (s._tag) {
                case "NotStarted": {
                    return (T.catchAllCause_(T.flatten(open(self)((pull) => ({ _tag: "Self", pull }))), failover));
                }
                case "Self": {
                    return (T.catchAllCause_(s.pull, failover));
                }
                case "Other": {
                    return s.pull;
                }
            }
        }));
    }), ({ pull }) => pull)));
}
exports.catchAllCause_ = catchAllCause_;
/**
 * Switches over to the stream produced by the provided function in case this one
 * fails. Allows recovery from all causes of failure, including interruption if the
 * stream is uninterruptible.
 */
function catchAllCause(f) {
    return (self) => catchAllCause_(self, f);
}
exports.catchAllCause = catchAllCause;
//# sourceMappingURL=catchAllCause.js.map