import { EnterNode } from "./enter.js";
import constant_default from "../constant.js";
import { Selection } from "./index.js";
function bindIndex(t, n, r, i, a, o) {
	for (var s = 0, c, l = n.length, u = o.length; s < u; ++s) (c = n[s]) ? (c.__data__ = o[s], i[s] = c) : r[s] = new EnterNode(t, o[s]);
	for (; s < l; ++s) (c = n[s]) && (a[s] = c);
}
function bindKey(t, n, r, i, a, o, s) {
	var c, l, u = /* @__PURE__ */ new Map(), d = n.length, f = o.length, p = Array(d), m;
	for (c = 0; c < d; ++c) (l = n[c]) && (p[c] = m = s.call(l, l.__data__, c, n) + "", u.has(m) ? a[c] = l : u.set(m, l));
	for (c = 0; c < f; ++c) m = s.call(t, o[c], c, o) + "", (l = u.get(m)) ? (i[c] = l, l.__data__ = o[c], u.delete(m)) : r[c] = new EnterNode(t, o[c]);
	for (c = 0; c < d; ++c) (l = n[c]) && u.get(p[c]) === l && (a[c] = l);
}
function datum(e) {
	return e.__data__;
}
function data_default(e, o) {
	if (!arguments.length) return Array.from(this, datum);
	var c = o ? bindKey : bindIndex, l = this._parents, u = this._groups;
	typeof e != "function" && (e = constant_default(e));
	for (var d = u.length, f = Array(d), p = Array(d), m = Array(d), h = 0; h < d; ++h) {
		var g = l[h], _ = u[h], v = _.length, y = arraylike(e.call(g, g && g.__data__, h, l)), b = y.length, x = p[h] = Array(b), S = f[h] = Array(b);
		c(g, _, x, S, m[h] = Array(v), y, o);
		for (var C = 0, w = 0, T, E; C < b; ++C) if (T = x[C]) {
			for (C >= w && (w = C + 1); !(E = S[w]) && ++w < b;);
			T._next = E || null;
		}
	}
	return f = new Selection(f, l), f._enter = p, f._exit = m, f;
}
function arraylike(e) {
	return typeof e == "object" && "length" in e ? e : Array.from(e);
}
export { data_default as default };
