"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.interleaveWith = exports.interleaveWith_ = void 0;
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const CH = __importStar(require("../../Channel"));
const TK = __importStar(require("../../Take"));
const C = __importStar(require("../core"));
const HO = __importStar(require("../Handoff"));
/**
 * Combines this stream and the specified stream deterministically using the
 * stream of boolean values `b` to control which stream to pull from next.
 * `true` indicates to pull from this stream and `false` indicates to pull
 * from the specified stream. Only consumes as many elements as requested by
 * `b`. If either this stream or the specified stream are exhausted further
 * requests for values from that stream will be ignored.
 */
function interleaveWith_(self, that, b) {
    const producer = (handoff) => CH.readWithCause((value) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, TK.single(value))), producer(handoff)), (cause) => CH.fromEffect(HO.offer(handoff, TK.failCause(cause))), (_) => CH.fromEffect(HO.offer(handoff, TK.end)));
    return new C.Stream(CH.managed_((M.map_(M.tap_(M.tap_(M.bind_(M.bind_(M.do, "left", () => T.toManaged(HO.make())), "right", () => T.toManaged(HO.make())), ({ left }) => M.fork(CH.runManaged(CH.concatMap_(self.channel, (_) => CH.writeChunk(_))[">>>"](producer(left))))), ({ right }) => M.fork(CH.runManaged(CH.concatMap_(that.channel, (_) => CH.writeChunk(_))[">>>"](producer(right))))), ({ left, right }) => Tp.tuple(left, right))), ({ tuple: [left, right] }) => {
        const process = (leftDone, rightDone) => CH.readWithCause((bool) => {
            if (bool && !leftDone) {
                return CH.chain_(CH.fromEffect(HO.take(left)), TK.fold(rightDone ? CH.unit : process(true, rightDone), (cause) => CH.failCause(cause), (chunk) => CH.zipRight_(CH.write(chunk), process(leftDone, rightDone))));
            }
            if (!bool && !rightDone) {
                return CH.chain_(CH.fromEffect(HO.take(right)), TK.fold(leftDone ? CH.unit : process(leftDone, true), (cause) => CH.failCause(cause), (chunk) => CH.zipRight_(CH.write(chunk), process(leftDone, rightDone))));
            }
            return process(leftDone, rightDone);
        }, (cause) => CH.failCause(cause), (_) => CH.unit);
        return CH.concatMap_(b.channel, (_) => CH.writeChunk(_))[">>>"](process(false, false));
    }));
}
exports.interleaveWith_ = interleaveWith_;
/**
 * Combines this stream and the specified stream deterministically using the
 * stream of boolean values `b` to control which stream to pull from next.
 * `true` indicates to pull from this stream and `false` indicates to pull
 * from the specified stream. Only consumes as many elements as requested by
 * `b`. If either this stream or the specified stream are exhausted further
 * requests for values from that stream will be ignored.
 *
 * @ets_data_first interleaveWith_
 */
function interleaveWith(that, b) {
    return (self) => interleaveWith_(self, that, b);
}
exports.interleaveWith = interleaveWith;
//# sourceMappingURL=interleaveWith.js.map