class u {
  constructor(e, t) {
    this.startFn = e, this.nextFn = t;
  }
  iterator() {
    const e = {
      state: this.startFn(),
      next: () => this.nextFn(e.state),
      [Symbol.iterator]: () => e
    };
    return e;
  }
  [Symbol.iterator]() {
    return this.iterator();
  }
  isEmpty() {
    return !!this.iterator().next().done;
  }
  count() {
    const e = this.iterator();
    let t = 0, r = e.next();
    for (; !r.done; )
      t++, r = e.next();
    return t;
  }
  toArray() {
    const e = [], t = this.iterator();
    let r;
    do
      r = t.next(), r.value !== void 0 && e.push(r.value);
    while (!r.done);
    return e;
  }
  toSet() {
    return new Set(this);
  }
  toMap(e, t) {
    const r = this.map((n) => [
      e ? e(n) : n,
      t ? t(n) : n
    ]);
    return new Map(r);
  }
  toString() {
    return this.join();
  }
  concat(e) {
    return new u(() => ({ first: this.startFn(), firstDone: !1, iterator: e[Symbol.iterator]() }), (t) => {
      let r;
      if (!t.firstDone) {
        do
          if (r = this.nextFn(t.first), !r.done)
            return r;
        while (!r.done);
        t.firstDone = !0;
      }
      do
        if (r = t.iterator.next(), !r.done)
          return r;
      while (!r.done);
      return l;
    });
  }
  join(e = ",") {
    const t = this.iterator();
    let r = "", n, i = !1;
    do
      n = t.next(), n.done || (i && (r += e), r += h(n.value)), i = !0;
    while (!n.done);
    return r;
  }
  indexOf(e, t = 0) {
    const r = this.iterator();
    let n = 0, i = r.next();
    for (; !i.done; ) {
      if (n >= t && i.value === e)
        return n;
      i = r.next(), n++;
    }
    return -1;
  }
  every(e) {
    const t = this.iterator();
    let r = t.next();
    for (; !r.done; ) {
      if (!e(r.value))
        return !1;
      r = t.next();
    }
    return !0;
  }
  some(e) {
    const t = this.iterator();
    let r = t.next();
    for (; !r.done; ) {
      if (e(r.value))
        return !0;
      r = t.next();
    }
    return !1;
  }
  forEach(e) {
    const t = this.iterator();
    let r = 0, n = t.next();
    for (; !n.done; )
      e(n.value, r), n = t.next(), r++;
  }
  map(e) {
    return new u(this.startFn, (t) => {
      const { done: r, value: n } = this.nextFn(t);
      return r ? l : { done: !1, value: e(n) };
    });
  }
  filter(e) {
    return new u(this.startFn, (t) => {
      let r;
      do
        if (r = this.nextFn(t), !r.done && e(r.value))
          return r;
      while (!r.done);
      return l;
    });
  }
  nonNullable() {
    return this.filter((e) => e != null);
  }
  reduce(e, t) {
    const r = this.iterator();
    let n = t, i = r.next();
    for (; !i.done; )
      n === void 0 ? n = i.value : n = e(n, i.value), i = r.next();
    return n;
  }
  reduceRight(e, t) {
    return this.recursiveReduce(this.iterator(), e, t);
  }
  recursiveReduce(e, t, r) {
    const n = e.next();
    if (n.done)
      return r;
    const i = this.recursiveReduce(e, t, r);
    return i === void 0 ? n.value : t(i, n.value);
  }
  find(e) {
    const t = this.iterator();
    let r = t.next();
    for (; !r.done; ) {
      if (e(r.value))
        return r.value;
      r = t.next();
    }
  }
  findIndex(e) {
    const t = this.iterator();
    let r = 0, n = t.next();
    for (; !n.done; ) {
      if (e(n.value))
        return r;
      n = t.next(), r++;
    }
    return -1;
  }
  includes(e) {
    const t = this.iterator();
    let r = t.next();
    for (; !r.done; ) {
      if (r.value === e)
        return !0;
      r = t.next();
    }
    return !1;
  }
  flatMap(e) {
    return new u(() => ({ this: this.startFn() }), (t) => {
      do {
        if (t.iterator) {
          const i = t.iterator.next();
          if (i.done)
            t.iterator = void 0;
          else
            return i;
        }
        const { done: r, value: n } = this.nextFn(t.this);
        if (!r) {
          const i = e(n);
          if (d(i))
            t.iterator = i[Symbol.iterator]();
          else
            return { done: !1, value: i };
        }
      } while (t.iterator);
      return l;
    });
  }
  flat(e) {
    if (e === void 0 && (e = 1), e <= 0)
      return this;
    const t = e > 1 ? this.flat(e - 1) : this;
    return new u(() => ({ this: t.startFn() }), (r) => {
      do {
        if (r.iterator) {
          const a = r.iterator.next();
          if (a.done)
            r.iterator = void 0;
          else
            return a;
        }
        const { done: n, value: i } = t.nextFn(r.this);
        if (!n)
          if (d(i))
            r.iterator = i[Symbol.iterator]();
          else
            return { done: !1, value: i };
      } while (r.iterator);
      return l;
    });
  }
  head() {
    const t = this.iterator().next();
    if (!t.done)
      return t.value;
  }
  tail(e = 1) {
    return new u(() => {
      const t = this.startFn();
      for (let r = 0; r < e; r++)
        if (this.nextFn(t).done)
          return t;
      return t;
    }, this.nextFn);
  }
  limit(e) {
    return new u(() => ({ size: 0, state: this.startFn() }), (t) => (t.size++, t.size > e ? l : this.nextFn(t.state)));
  }
  distinct(e) {
    return new u(() => ({ set: /* @__PURE__ */ new Set(), internalState: this.startFn() }), (t) => {
      let r;
      do
        if (r = this.nextFn(t.internalState), !r.done) {
          const n = e ? e(r.value) : r.value;
          if (!t.set.has(n))
            return t.set.add(n), r;
        }
      while (!r.done);
      return l;
    });
  }
  exclude(e, t) {
    const r = /* @__PURE__ */ new Set();
    for (const n of e) {
      const i = t ? t(n) : n;
      r.add(i);
    }
    return this.filter((n) => {
      const i = t ? t(n) : n;
      return !r.has(i);
    });
  }
}
function h(o) {
  return typeof o == "string" ? o : typeof o > "u" ? "undefined" : typeof o.toString == "function" ? o.toString() : Object.prototype.toString.call(o);
}
function d(o) {
  return !!o && typeof o[Symbol.iterator] == "function";
}
const x = new u(() => {
}, () => l), l = Object.freeze({ done: !0, value: void 0 });
function c(...o) {
  if (o.length === 1) {
    const e = o[0];
    if (e instanceof u)
      return e;
    if (d(e))
      return new u(() => e[Symbol.iterator](), (t) => t.next());
    if (typeof e.length == "number")
      return new u(() => ({ index: 0 }), (t) => t.index < e.length ? { done: !1, value: e[t.index++] } : l);
  }
  return o.length > 1 ? new u(() => ({ collIndex: 0, arrIndex: 0 }), (e) => {
    do {
      if (e.iterator) {
        const t = e.iterator.next();
        if (!t.done)
          return t;
        e.iterator = void 0;
      }
      if (e.array) {
        if (e.arrIndex < e.array.length)
          return { done: !1, value: e.array[e.arrIndex++] };
        e.array = void 0, e.arrIndex = 0;
      }
      if (e.collIndex < o.length) {
        const t = o[e.collIndex++];
        d(t) ? e.iterator = t[Symbol.iterator]() : t && typeof t.length == "number" && (e.array = t);
      }
    } while (e.iterator || e.array || e.collIndex < o.length);
    return l;
  }) : x;
}
class v extends u {
  constructor(e, t, r) {
    super(() => ({
      iterators: r != null && r.includeRoot ? [[e][Symbol.iterator]()] : [t(e)[Symbol.iterator]()],
      pruned: !1
    }), (n) => {
      for (n.pruned && (n.iterators.pop(), n.pruned = !1); n.iterators.length > 0; ) {
        const a = n.iterators[n.iterators.length - 1].next();
        if (a.done)
          n.iterators.pop();
        else
          return n.iterators.push(t(a.value)[Symbol.iterator]()), a;
      }
      return l;
    });
  }
  iterator() {
    const e = {
      state: this.startFn(),
      next: () => this.nextFn(e.state),
      prune: () => {
        e.state.pruned = !0;
      },
      [Symbol.iterator]: () => e
    };
    return e;
  }
}
var f;
(function(o) {
  function e(i) {
    return i.reduce((a, s) => a + s, 0);
  }
  o.sum = e;
  function t(i) {
    return i.reduce((a, s) => a * s, 0);
  }
  o.product = t;
  function r(i) {
    return i.reduce((a, s) => Math.min(a, s));
  }
  o.min = r;
  function n(i) {
    return i.reduce((a, s) => Math.max(a, s));
  }
  o.max = n;
})(f || (f = {}));
export {
  l as DONE_RESULT,
  x as EMPTY_STREAM,
  f as Reduction,
  u as StreamImpl,
  v as TreeStreamImpl,
  c as stream
};
