import { EMPTY_STREAM, Reduction, stream } from "./stream.js";
var MultiMap = class {
	constructor(e) {
		if (this.map = /* @__PURE__ */ new Map(), e) for (let [n, r] of e) this.add(n, r);
	}
	get size() {
		return Reduction.sum(stream(this.map.values()).map((e) => e.length));
	}
	clear() {
		this.map.clear();
	}
	delete(e, n) {
		if (n === void 0) return this.map.delete(e);
		{
			let r = this.map.get(e);
			if (r) {
				let i = r.indexOf(n);
				if (i >= 0) return r.length === 1 ? this.map.delete(e) : r.splice(i, 1), !0;
			}
			return !1;
		}
	}
	get(e) {
		return this.map.get(e) ?? [];
	}
	getStream(n) {
		let i = this.map.get(n);
		return i ? stream(i) : EMPTY_STREAM;
	}
	has(e, n) {
		if (n === void 0) return this.map.has(e);
		{
			let r = this.map.get(e);
			return r ? r.indexOf(n) >= 0 : !1;
		}
	}
	add(e, n) {
		return this.map.has(e) ? this.map.get(e).push(n) : this.map.set(e, [n]), this;
	}
	addAll(e, n) {
		return this.map.has(e) ? this.map.get(e).push(...n) : this.map.set(e, Array.from(n)), this;
	}
	forEach(e) {
		this.map.forEach((n, r) => n.forEach((n) => e(n, r, this)));
	}
	[Symbol.iterator]() {
		return this.entries().iterator();
	}
	entries() {
		return stream(this.map.entries()).flatMap(([e, n]) => n.map((n) => [e, n]));
	}
	keys() {
		return stream(this.map.keys());
	}
	values() {
		return stream(this.map.values()).flat();
	}
	entriesGroupedByKey() {
		return stream(this.map.entries());
	}
}, BiMap = class {
	get size() {
		return this.map.size;
	}
	constructor(e) {
		if (this.map = /* @__PURE__ */ new Map(), this.inverse = /* @__PURE__ */ new Map(), e) for (let [n, r] of e) this.set(n, r);
	}
	clear() {
		this.map.clear(), this.inverse.clear();
	}
	set(e, n) {
		return this.map.set(e, n), this.inverse.set(n, e), this;
	}
	get(e) {
		return this.map.get(e);
	}
	getKey(e) {
		return this.inverse.get(e);
	}
	delete(e) {
		let n = this.map.get(e);
		return n === void 0 ? !1 : (this.map.delete(e), this.inverse.delete(n), !0);
	}
};
export { BiMap, MultiMap };
