"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.combineChunks = exports.combineChunks_ = 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"));
const UnfoldChunkEffect = __importStar(require("./unfoldChunkEffect"));
/**
 * Combines the chunks from this stream and the specified stream by repeatedly applying the
 * function `f` to extract a chunk 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`.
 */
function combineChunks_(self, that, s, f) {
    const producer = (handoff, latch) => CH.zipRight_(CH.fromEffect(HO.take(latch)), CH.readWithCause((chunk) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, TK.chunk(chunk))), producer(handoff, latch)), (cause) => CH.fromEffect(HO.offer(handoff, TK.failCause(cause))), (_) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, TK.end)), 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(self.channel[">>>"](producer(left, latchL)))))), ({ latchR, right }) => (M.fork(CH.runManaged(that.channel[">>>"](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), TK.done));
        const pullRight = T.zipRight_(HO.offer(latchR, undefined), T.chain_(HO.take(right), TK.done));
        return UnfoldChunkEffect.unfoldChunkEffect(s, (s) => T.chain_(f(s, pullLeft, pullRight), (_) => T.unoption(T.done(_)))).channel;
    }));
}
exports.combineChunks_ = combineChunks_;
/**
 * Combines the chunks from this stream and the specified stream by repeatedly applying the
 * function `f` to extract a chunk 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`.
 *
 * @ets_data_first combineChunks_
 */
function combineChunks(that, s, f) {
    return (self) => combineChunks_(self, that, s, f);
}
exports.combineChunks = combineChunks;
//# sourceMappingURL=combineChunks.js.map