"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.combine = exports.combine_ = void 0;
// ets_tracing: off
const CS = __importStar(require("../../../../Cause"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const Ex = __importStar(require("../../../../Exit"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const O = __importStar(require("../../../../Option"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
const HO = __importStar(require("../Handoff"));
const UnfoldEffect = __importStar(require("./unfoldEffect"));
/**
 * Combines the elements from this stream and the specified stream by repeatedly applying the
 * function `f` to extract an element using both sides and conceptually "offer"
 * it to the destination stream. `f` can maintain some internal state to control
 * the combining process, with the initial state being specified by `s`.
 *
 * Where possible, prefer `Stream#combineChunks` for a more efficient implementation.
 */
function combine_(self, that, s, f) {
    const producer = (handoff, latch) => CH.zipRight_(CH.fromEffect(HO.take(latch)), CH.readWithCause((value) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, Ex.succeed(value))), producer(handoff, latch)), (cause) => CH.fromEffect(HO.offer(handoff, Ex.failCause(CS.map_(cause, O.some)))), (_) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, Ex.fail(O.none))), producer(handoff, latch))));
    return new C.Stream(CH.managed_((M.map_(M.tap_(M.tap_(M.bind_(M.bind_(M.bind_(M.bind_(M.do, "left", () => T.toManaged(HO.make())), "right", () => T.toManaged(HO.make())), "latchL", () => T.toManaged(HO.make())), "latchR", () => T.toManaged(HO.make())), ({ latchL, left }) => (M.fork(CH.runManaged(CH.concatMap_(self.channel, (_) => CH.writeChunk(_))[">>>"](producer(left, latchL)))))), ({ latchR, right }) => (M.fork(CH.runManaged(CH.concatMap_(that.channel, (_) => CH.writeChunk(_))[">>>"](producer(right, latchR)))))), ({ latchL, latchR, left, right }) => Tp.tuple(left, right, latchL, latchR))), ({ tuple: [left, right, latchL, latchR] }) => {
        const pullLeft = T.zipRight_(HO.offer(latchL, undefined), T.chain_(HO.take(left), T.done));
        const pullRight = T.zipRight_(HO.offer(latchR, undefined), T.chain_(HO.take(right), T.done));
        return UnfoldEffect.unfoldEffect(s, (s) => T.chain_(f(s, pullLeft, pullRight), (_) => T.unoption(T.done(_)))).channel;
    }));
}
exports.combine_ = combine_;
/**
 * Combines the elements from this stream and the specified stream by repeatedly applying the
 * function `f` to extract an element using both sides and conceptually "offer"
 * it to the destination stream. `f` can maintain some internal state to control
 * the combining process, with the initial state being specified by `s`.
 *
 * Where possible, prefer `Stream#combineChunks` for a more efficient implementation.
 *
 * @ets_data_first combine_
 */
function combine(that, s, f) {
    return (self) => combine_(self, that, s, f);
}
exports.combine = combine;
//# sourceMappingURL=combine.js.map