"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.in = exports.in_ = void 0;
// ets_tracing: off
const Array_1 = require("../Collections/Immutable/Array");
const Fiber = __importStar(require("../Fiber"));
const Function_1 = require("../Function");
const O = __importStar(require("../Option"));
const core = __importStar(require("./core"));
const core_scope_1 = require("./core-scope");
const interruption = __importStar(require("./interruption"));
/**
 * Returns a new effect whose scope will be extended by the specified scope.
 * This means any finalizers associated with the effect will not be executed
 * until the specified scope is closed.
 *
 * @ets_data_first in_
 */
function _in(scope, __trace) {
    return (self) => in_(self, scope, __trace);
}
exports.in = _in;
/**
 * Returns a new effect whose scope will be extended by the specified scope.
 * This means any finalizers associated with the effect will not be executed
 * until the specified scope is closed.
 */
function in_(self, scope, __trace) {
    return interruption.uninterruptibleMask(({ restore }) => (core.chain_((0, core_scope_1.forkDaemon)(restore(self), __trace), (fiber) => (core.chain_(scope.extend(fiber.scope), () => (interruption.onInterrupt_(restore(Fiber.join(fiber)), (x) => (O.fold_((0, Array_1.head)(Array.from(x)), () => Fiber.interrupt(fiber), (id) => fiber.interruptAs(id))))))))));
}
exports.in_ = in_;
//# sourceMappingURL=in.js.map