"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 = exports.zipWithLatest_ = void 0;
// ets_tracing: off
const A = __importStar(require("../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../Collections/Immutable/Tuple"));
const E = __importStar(require("../../Either"));
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 mapm = __importStar(require("./mapM"));
const merge = __importStar(require("./merge"));
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, f) {
    return 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((latest) => (concat.concat((chain.chain(fromChunk_1.fromChunk)(mapm.mapM(E.fold((leftChunk) => Ref.modify_(latest, ({ tuple: [_, rightLatest] }) => Tp.tuple(A.map_(leftChunk, (_) => f(_, rightLatest)), Tp.tuple(A.unsafeGet_(leftChunk, A.size(leftChunk) - 1), rightLatest))), (rightChunk) => Ref.modify_(latest, ({ tuple: [leftLatest, _] }) => Tp.tuple(A.map_(rightChunk, (_) => f(leftLatest, _)), Tp.tuple(leftLatest, A.unsafeGet_(rightChunk, A.size(rightChunk) - 1))))))(merge.mergeEither_((0, repeatEffectOption_1.repeatEffectOption)(left), (0, repeatEffectOption_1.repeatEffectOption)(right))))))((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)(Ref.makeRef(Tp.tuple(A.unsafeGet_(l, A.size(l) - 1), 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), (l, r) => Tp.tuple(l, r, true))), (rightDone, leftFiber) => (T.zipWith_(T.done(rightDone), F.join(leftFiber), (r, l) => Tp.tuple(l, r, false))))))).proc), ({ pull }) => pull)));
}
exports.zipWithLatest_ = zipWithLatest_;
/**
 * 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(that, f) {
    return (self) => zipWithLatest_(self, that, f);
}
exports.zipWithLatest = zipWithLatest;
//# sourceMappingURL=zipWithLatest.js.map