"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 CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const E = __importStar(require("../../../../Either"));
const F = __importStar(require("../../../../Fiber"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const Ref = __importStar(require("../../../../Ref"));
const Chain = __importStar(require("./chain"));
const Concat = __importStar(require("./concat"));
const FromChunk = __importStar(require("./fromChunk"));
const FromEffect = __importStar(require("./fromEffect"));
const FromEffectOption = __importStar(require("./fromEffectOption"));
const FromPull = __importStar(require("./fromPull"));
const MapEffect = __importStar(require("./mapEffect"));
const MergeEither = __importStar(require("./mergeEither"));
const RepeatEffectOption = __importStar(require("./repeatEffectOption"));
const ToPull = __importStar(require("./toPull"));
/**
 * 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) {
    const pullNonEmpty = (pull) => T.chain_(pull, (chunk) => CK.isEmpty(chunk) ? pullNonEmpty(pull) : T.succeed(chunk));
    return FromPull.fromPull((M.map_(M.bind_(M.bind_(M.bind_(M.do, "left", () => M.map_(ToPull.toPull(self), pullNonEmpty)), "right", () => M.map_(ToPull.toPull(that), pullNonEmpty)), "pull", ({ left, right }) => ToPull.toPull(Chain.chain_(FromEffectOption.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)))), ({ tuple: [l, r, leftFirst] }) => Chain.chain_(FromEffect.fromEffect(Ref.makeRef(Tp.tuple(CK.unsafeGet_(l, CK.size(l) - 1), CK.unsafeGet_(r, CK.size(r) - 1)))), (latest) => Concat.concat_(FromChunk.fromChunk(leftFirst
        ? CK.map_(r, (_) => f(CK.unsafeGet_(l, CK.size(l) - 1), _))
        : CK.map_(l, (_) => f(_, CK.unsafeGet_(r, CK.size(r) - 1)))), (Chain.chain_(MapEffect.mapEffect_(MergeEither.mergeEither_(RepeatEffectOption.repeatEffectOption(left), RepeatEffectOption.repeatEffectOption(right)), E.fold((leftChunk) => Ref.modify_(latest, ({ tuple: [_, rightLatest] }) => Tp.tuple(CK.map_(leftChunk, (_) => f(_, rightLatest)), Tp.tuple(CK.unsafeGet_(leftChunk, CK.size(leftChunk) - 1), rightLatest))), (rightChunk) => Ref.modify_(latest, ({ tuple: [leftLatest, _] }) => Tp.tuple(CK.map_(rightChunk, (_) => f(leftLatest, _)), Tp.tuple(leftLatest, CK.unsafeGet_(rightChunk, CK.size(rightChunk) - 1)))))), FromChunk.fromChunk))))))), ({ 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.
 *
 * @ets_data_first zipWithLatest_
 */
function zipWithLatest(that, f) {
    return (self) => zipWithLatest_(self, that, f);
}
exports.zipWithLatest = zipWithLatest;
//# sourceMappingURL=zipWithLatest.js.map