"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.asyncInterrupt = void 0;
// ets_tracing: off
const CS = __importStar(require("../../../../Cause"));
const T = __importStar(require("../../../../Effect"));
const E = __importStar(require("../../../../Either"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const O = __importStar(require("../../../../Option"));
const Q = __importStar(require("../../../../Queue"));
const CH = __importStar(require("../../Channel"));
const TK = __importStar(require("../../Take"));
const C = __importStar(require("../core"));
const Emit_1 = require("./_internal/Emit");
const Ensuring = __importStar(require("./ensuring"));
const Unwrap = __importStar(require("./unwrap"));
const UnwrapManaged = __importStar(require("./unwrapManaged"));
/**
 * Creates a stream from an asynchronous callback that can be called multiple times.
 * The registration of the callback returns either a canceler or synchronously returns a stream.
 * The optionality of the error type `E` can be used to signal the end of the stream, by
 * setting it to `None`.
 */
function asyncInterrupt(register, outputBuffer = 16) {
    return UnwrapManaged.unwrapManaged((M.map_(M.bind_(M.bind_(M.bind_(M.do, "output", () => T.toManagedRelease_(Q.makeBounded(outputBuffer), Q.shutdown)), "runtime", () => M.runtime()), "eitherStream", ({ output, runtime }) => M.succeed(register((0, Emit_1.toEmit)((k) => {
        try {
            runtime.run(T.chain_(TK.fromPull(k), (_) => Q.offer_(output, _)));
        }
        catch (e) {
            if (CS.isFiberFailure(e)) {
                if (!CS.interrupted(e.cause)) {
                    throw e;
                }
            }
        }
    })))), ({ eitherStream, output }) => E.fold_(eitherStream, (canceler) => {
        const loop = CH.unwrap((T.fold_(T.chain_(Q.take(output), (_) => TK.done(_)), (maybeError) => CH.zipRight_(CH.fromEffect(Q.shutdown(output)), O.fold_(maybeError, () => CH.end(undefined), (_) => CH.fail(_))), (a) => CH.zipRight_(CH.write(a), loop))));
        return Ensuring.ensuring_(new C.Stream(loop), canceler);
    }, (value) => Unwrap.unwrap(T.as_(Q.shutdown(output), value))))));
}
exports.asyncInterrupt = asyncInterrupt;
//# sourceMappingURL=asyncInterrupt.js.map