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