"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.zipWithLatest = void 0;
exports.zipWithLatest_ = zipWithLatest_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/size"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/unsafeGet"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/map"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromChunk"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/fold"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/repeatEffectMaybe"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/mergeEither"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/mapEffect"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/flatMap"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/concat"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Ref/operations/make"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromEffect"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/join"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/done"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipWith"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/raceWith"));

var tsplus_module_18 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromEffectMaybe"));

var tsplus_module_19 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/toPull"));

var tsplus_module_20 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_21 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/do"));

var tsplus_module_22 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_23 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromPull"));

var tsplus_module_24 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/isEmpty"));

var tsplus_module_25 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeedNow"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * 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.
 *
 * @tsplus fluent ets/Stream zipWithLatest
 */
function zipWithLatest_(self, that, f, __tsplusTrace) {
  return tsplus_module_23.fromPull(() => tsplus_module_22.flatMap_(tsplus_module_21.bind_(tsplus_module_21.bind_(tsplus_module_21.Do(), "left", () => tsplus_module_20.map_(tsplus_module_19.toPull(self, __tsplusTrace), pullNonEmpty, __tsplusTrace), __tsplusTrace), "right", () => tsplus_module_20.map_(tsplus_module_19.toPull(that(), __tsplusTrace), pullNonEmpty, __tsplusTrace), __tsplusTrace), ({
    left,
    right
  }) => tsplus_module_19.toPull(tsplus_module_10.flatMap_(tsplus_module_18.fromEffectMaybe(() => tsplus_module_17.raceWith_(left, () => right, (leftDone, rightFiber) => tsplus_module_16.zipWith_(tsplus_module_15.done(() => leftDone, __tsplusTrace), () => tsplus_module_14.join(rightFiber, __tsplusTrace), (left, right) => tsplus_module_5.make(left, right, true), __tsplusTrace), (rightDone, leftFiber) => tsplus_module_16.zipWith_(tsplus_module_15.done(() => rightDone, __tsplusTrace), () => tsplus_module_14.join(leftFiber, __tsplusTrace), (right, left) => tsplus_module_5.make(left, right, false), __tsplusTrace), __tsplusTrace), __tsplusTrace), ({
    tuple: [l, r, leftFirst]
  }) => tsplus_module_10.flatMap_(tsplus_module_13.fromEffect(() => tsplus_module_12.makeRef(() => tsplus_module_5.make(tsplus_module_2.unsafeGet_(l, tsplus_module_1.size(l) - 1), tsplus_module_2.unsafeGet_(r, tsplus_module_1.size(r) - 1)), __tsplusTrace), __tsplusTrace), latest => tsplus_module_11.concat_(tsplus_module_4.fromChunk(() => leftFirst ? tsplus_module_3.map_(r, a2 => f(tsplus_module_2.unsafeGet_(l, tsplus_module_1.size(l) - 1), a2)) : tsplus_module_3.map_(l, a => f(a, tsplus_module_2.unsafeGet_(r, tsplus_module_1.size(r) - 1))), __tsplusTrace), () => tsplus_module_10.flatMap_(tsplus_module_9.mapEffect_(tsplus_module_8.mergeEither_(tsplus_module_7.repeatEffectMaybe(() => left, __tsplusTrace), () => tsplus_module_7.repeatEffectMaybe(() => right, __tsplusTrace)), either => tsplus_module_6.fold_(either, leftChunk => latest.modify(({
    tuple: [_, rightLatest]
  }) => tsplus_module_5.make(tsplus_module_3.map_(leftChunk, a => f(a, rightLatest)), tsplus_module_5.make(tsplus_module_2.unsafeGet_(leftChunk, tsplus_module_1.size(leftChunk) - 1), rightLatest)), __tsplusTrace), rightChunk => latest.modify(({
    tuple: [leftLatest, _]
  }) => tsplus_module_5.make(tsplus_module_3.map_(rightChunk, a2 => f(leftLatest, a2)), tsplus_module_5.make(leftLatest, tsplus_module_2.unsafeGet_(rightChunk, tsplus_module_1.size(rightChunk) - 1))), __tsplusTrace)), __tsplusTrace), chunk => tsplus_module_4.fromChunk(() => chunk, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace);
}
/**
 * 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.
 *
 * @tsplus static ets/Stream/Aspects zipWithLatest
 */


const zipWithLatest = (that, f, __tsplusTrace) => self => zipWithLatest_(self, that, f, __tsplusTrace);

exports.zipWithLatest = zipWithLatest;

function pullNonEmpty(pull, __tsplusTrace) {
  return tsplus_module_22.flatMap_(pull, chunk => tsplus_module_24.isEmpty(chunk) ? pullNonEmpty(pull, __tsplusTrace) : tsplus_module_25.succeedNow(chunk, __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=zipWithLatest.js.map