"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bracketExit_ = exports.bracketExit = void 0;
// ets_tracing: off
const cause_1 = require("../Cause/cause");
const api_1 = require("../Exit/api");
const core_1 = require("./core");
const done_1 = require("./done");
const interruption_1 = require("./interruption");
/**
 * Acquires a resource, uses the resource, and then releases the resource.
 * Neither the acquisition nor the release will be interrupted, and the
 * resource is guaranteed to be released, so long as the `acquire` effect
 * succeeds. If `use` fails, then after release, the returned effect will fail
 * with the same error.
 *
 * @ets_data_first bracketExit_
 */
function bracketExit(use, release, __trace) {
    return (acquire) => bracketExit_(acquire, use, release, __trace);
}
exports.bracketExit = bracketExit;
/**
 * Acquires a resource, uses the resource, and then releases the resource.
 * Neither the acquisition nor the release will be interrupted, and the
 * resource is guaranteed to be released, so long as the `acquire` effect
 * succeeds. If `use` fails, then after release, the returned effect will fail
 * with the same error.
 */
function bracketExit_(acquire, use, release, __trace) {
    return (0, interruption_1.uninterruptibleMask)(({ restore }) => (0, core_1.chain_)(acquire, (a) => (0, core_1.chain_)((0, core_1.result)(restore(use(a))), (e) => (0, core_1.foldCauseM_)(release(a, e), (cause2) => (0, core_1.halt)((0, api_1.fold_)(e, (_) => (0, cause_1.then)(_, cause2), (_) => cause2)), (_) => (0, done_1.done)(e))), __trace));
}
exports.bracketExit_ = bracketExit_;
//# sourceMappingURL=bracketExit.js.map