"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;
// 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 O = __importStar(require("../../Option"));
const BP = __importStar(require("../../Stream/BufferedPull"));
const T = __importStar(require("../_internal/effect"));
const M = __importStar(require("../_internal/managed"));
const combine_1 = require("./combine");
const definitions_1 = require("./definitions");
function loop(leftDone, rightDone, s, left, right) {
    return T.foldCauseM_(s, (_) => O.fold_(C.sequenceCauseOption(_), () => T.succeed(Ex.fail(O.none)), (e) => T.succeed(Ex.halt(C.map_(e, O.some)))), (b) => {
        if (b && !leftDone) {
            return T.foldCauseM_(left, (_) => O.fold_(C.sequenceCauseOption(_), () => {
                if (rightDone) {
                    return T.succeed(Ex.fail(O.none));
                }
                else {
                    return loop(true, rightDone, s, left, right);
                }
            }, (e) => T.succeed(Ex.halt(C.map_(e, O.some)))), (a) => T.succeed(Ex.succeed(Tp.tuple(a, Tp.tuple(leftDone, rightDone, s)))));
        }
        else if (!b && !rightDone) {
            return T.foldCauseM_(right, (_) => O.fold_(C.sequenceCauseOption(_), () => {
                if (leftDone) {
                    return T.succeed(Ex.fail(O.none));
                }
                else {
                    return loop(leftDone, true, s, left, right);
                }
            }, (e) => T.succeed(Ex.halt(C.map_(e, O.some)))), (a) => T.succeed(Ex.succeed(Tp.tuple(a, Tp.tuple(leftDone, rightDone, s)))));
        }
        else {
            return loop(leftDone, rightDone, s, left, right);
        }
    });
}
/**
 * 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) {
    return new definitions_1.Stream((M.map_(M.bind_(M.bind_(M.do, "sides", () => M.mapM_(b.proc, BP.make)), "result", ({ sides }) => ((0, combine_1.combine_)(self, that, Tp.tuple(false, false, BP.pullElement(sides)), ({ tuple: [leftDone, rightDone, sides] }, left, right) => {
        return loop(leftDone, rightDone, sides, left, right);
    })).proc), ({ result }) => result)));
}
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