"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.zipWithLatest = void 0;
// ets_tracing: off
const A = __importStar(require("../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../Collections/Immutable/Tuple"));
const Function_1 = require("../../Function");
const T = __importStar(require("../_internal/effect"));
const F = __importStar(require("../_internal/fiber"));
const M = __importStar(require("../_internal/managed"));
const Ref = __importStar(require("../_internal/ref"));
const chain = __importStar(require("./chain"));
const concat = __importStar(require("./concat"));
const definitions_1 = require("./definitions");
const fromChunk_1 = require("./fromChunk");
const fromEffect_1 = require("./fromEffect");
const fromEffectOption_1 = require("./fromEffectOption");
const mergeWith = __importStar(require("./mergeWith"));
const repeatEffectOption_1 = require("./repeatEffectOption");
function pullNonEmpty(pull) {
    return T.chain_(pull, (chunk) => A.isEmpty(chunk) ? pullNonEmpty(pull) : T.succeed(chunk));
}
/**
 * Zips the two streams so that when a value is emitted by either of the two streams,
 * it is combined with the latest value from the other stream to produce a result.
 *
 * Note: tracking the latest value is done on a per-chunk basis. That means that
 * emitted elements that are not the last value in chunks will never be used for zipping.
 */
function zipWithLatest(self, that) {
    return (f) => new definitions_1.Stream((M.map_(M.bind_(M.bind_(M.bind_(M.do, "left", () => M.map_(self.proc, pullNonEmpty)), "right", () => M.map_(that.proc, pullNonEmpty)), "pull", ({ left, right }) => (chain.chain(({ tuple: [l, r, leftFirst] }) => (chain.chain(({ tuple: [latestLeft, latestRight] }) => (concat.concat((chain.chain(fromChunk_1.fromChunk)(mergeWith.mergeWith_((0, repeatEffectOption_1.repeatEffectOption)((T.zip_(T.tap_(left, (chunk) => latestLeft.set(A.unsafeGet_(chunk, A.size(chunk) - 1))), latestRight.get))), (0, repeatEffectOption_1.repeatEffectOption)((T.zip_(T.tap_(right, (chunk) => latestRight.set(A.unsafeGet_(chunk, A.size(chunk) - 1))), latestLeft.get))), ({ tuple: [leftChunk, rightLatest] }) => A.map_(leftChunk, (_) => f(_, rightLatest)), ({ tuple: [rightChunk, leftLatest] }) => A.map_(rightChunk, (_) => f(leftLatest, _))))))((0, fromChunk_1.fromChunk)(leftFirst
        ? A.map_(r, (_) => f(A.unsafeGet_(l, A.size(l) - 1), _))
        : A.map_(l, (_) => f(_, A.unsafeGet_(r, A.size(r) - 1)))))))((0, fromEffect_1.fromEffect)(T.zip_(Ref.makeRef(A.unsafeGet_(l, A.size(l) - 1)), Ref.makeRef(A.unsafeGet_(r, A.size(r) - 1)))))))((0, fromEffectOption_1.fromEffectOption)(T.raceWith_(left, right, (leftDone, rightFiber) => (T.zipWith_(T.done(leftDone), F.join(rightFiber), (a, b) => Tp.tuple(a, b, true))), (rightDone, leftFiber) => (T.zipWith_(T.done(rightDone), F.join(leftFiber), (r, l) => Tp.tuple(l, r, false))))))).proc), ({ pull }) => pull)));
}
exports.zipWithLatest = zipWithLatest;
//# sourceMappingURL=zipWithLatest.js.map