import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/size";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/map";
import * as tsplus_module_4 from "@effect/core/stream/Stream/operations/fromChunk";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_6 from "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_7 from "@effect/core/stream/Stream/operations/repeatEffectMaybe";
import * as tsplus_module_8 from "@effect/core/stream/Stream/operations/mergeEither";
import * as tsplus_module_9 from "@effect/core/stream/Stream/operations/mapEffect";
import * as tsplus_module_10 from "@effect/core/stream/Stream/operations/flatMap";
import * as tsplus_module_11 from "@effect/core/stream/Stream/operations/concat";
import * as tsplus_module_12 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_13 from "@effect/core/stream/Stream/operations/fromEffect";
import * as tsplus_module_14 from "@effect/core/io/Fiber/operations/join";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/done";
import * as tsplus_module_16 from "@effect/core/io/Effect/operations/zipWith";
import * as tsplus_module_17 from "@effect/core/io/Effect/operations/raceWith";
import * as tsplus_module_18 from "@effect/core/stream/Stream/operations/fromEffectMaybe";
import * as tsplus_module_19 from "@effect/core/stream/Stream/operations/toPull";
import * as tsplus_module_20 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_21 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_22 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_23 from "@effect/core/stream/Stream/operations/fromPull";
import * as tsplus_module_24 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_25 from "@effect/core/io/Effect/operations/succeedNow";
/**
 * 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
 */

export 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
 */

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

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.mjs.map