import e from "../node_modules/d3-array/src/max.js";
import t from "../node_modules/d3-array/src/min.js";
import n from "../node_modules/d3-array/src/sum.js";
import { justify as r } from "./align.js";
import i from "./constant.js";
//#region ../../node_modules/d3-sankey/src/sankey.js
function a(e, t) {
	return s(e.source, t.source) || e.index - t.index;
}
function o(e, t) {
	return s(e.target, t.target) || e.index - t.index;
}
function s(e, t) {
	return e.y0 - t.y0;
}
function c(e) {
	return e.value;
}
function l(e) {
	return e.index;
}
function u(e) {
	return e.nodes;
}
function d(e) {
	return e.links;
}
function f(e, t) {
	let n = e.get(t);
	if (!n) throw Error("missing: " + t);
	return n;
}
function p({ nodes: e }) {
	for (let t of e) {
		let e = t.y0, n = e;
		for (let n of t.sourceLinks) n.y0 = e + n.width / 2, e += n.width;
		for (let e of t.targetLinks) e.y1 = n + e.width / 2, n += e.width;
	}
}
function m() {
	let m = 0, h = 0, g = 1, _ = 1, v = 24, y = 8, b, x = l, S = r, C, w, T = u, E = d, D = 6;
	function O() {
		let e = {
			nodes: T.apply(null, arguments),
			links: E.apply(null, arguments)
		};
		return k(e), A(e), j(e), M(e), F(e), p(e), e;
	}
	O.update = function(e) {
		return p(e), e;
	}, O.nodeId = function(e) {
		return arguments.length ? (x = typeof e == "function" ? e : i(e), O) : x;
	}, O.nodeAlign = function(e) {
		return arguments.length ? (S = typeof e == "function" ? e : i(e), O) : S;
	}, O.nodeSort = function(e) {
		return arguments.length ? (C = e, O) : C;
	}, O.nodeWidth = function(e) {
		return arguments.length ? (v = +e, O) : v;
	}, O.nodePadding = function(e) {
		return arguments.length ? (y = b = +e, O) : y;
	}, O.nodes = function(e) {
		return arguments.length ? (T = typeof e == "function" ? e : i(e), O) : T;
	}, O.links = function(e) {
		return arguments.length ? (E = typeof e == "function" ? e : i(e), O) : E;
	}, O.linkSort = function(e) {
		return arguments.length ? (w = e, O) : w;
	}, O.size = function(e) {
		return arguments.length ? (m = h = 0, g = +e[0], _ = +e[1], O) : [g - m, _ - h];
	}, O.extent = function(e) {
		return arguments.length ? (m = +e[0][0], g = +e[1][0], h = +e[0][1], _ = +e[1][1], O) : [[m, h], [g, _]];
	}, O.iterations = function(e) {
		return arguments.length ? (D = +e, O) : D;
	};
	function k({ nodes: e, links: t }) {
		for (let [t, n] of e.entries()) n.index = t, n.sourceLinks = [], n.targetLinks = [];
		let n = new Map(e.map((t, n) => [x(t, n, e), t]));
		for (let [e, r] of t.entries()) {
			r.index = e;
			let { source: t, target: i } = r;
			typeof t != "object" && (t = r.source = f(n, t)), typeof i != "object" && (i = r.target = f(n, i)), t.sourceLinks.push(r), i.targetLinks.push(r);
		}
		if (w != null) for (let { sourceLinks: t, targetLinks: n } of e) t.sort(w), n.sort(w);
	}
	function A({ nodes: e }) {
		for (let t of e) t.value = t.fixedValue === void 0 ? Math.max(n(t.sourceLinks, c), n(t.targetLinks, c)) : t.fixedValue;
	}
	function j({ nodes: e }) {
		let t = e.length, n = new Set(e), r = /* @__PURE__ */ new Set(), i = 0;
		for (; n.size;) {
			for (let e of n) {
				e.depth = i;
				for (let { target: t } of e.sourceLinks) r.add(t);
			}
			if (++i > t) throw Error("circular link");
			n = r, r = /* @__PURE__ */ new Set();
		}
	}
	function M({ nodes: e }) {
		let t = e.length, n = new Set(e), r = /* @__PURE__ */ new Set(), i = 0;
		for (; n.size;) {
			for (let e of n) {
				e.height = i;
				for (let { source: t } of e.targetLinks) r.add(t);
			}
			if (++i > t) throw Error("circular link");
			n = r, r = /* @__PURE__ */ new Set();
		}
	}
	function N({ nodes: t }) {
		let n = e(t, (e) => e.depth) + 1, r = (g - m - v) / (n - 1), i = Array(n);
		for (let e of t) {
			let t = Math.max(0, Math.min(n - 1, Math.floor(S.call(null, e, n))));
			e.layer = t, e.x0 = m + t * r, e.x1 = e.x0 + v, i[t] ? i[t].push(e) : i[t] = [e];
		}
		if (C) for (let e of i) e.sort(C);
		return i;
	}
	function P(e) {
		let r = t(e, (e) => (_ - h - (e.length - 1) * b) / n(e, c));
		for (let t of e) {
			let e = h;
			for (let n of t) {
				n.y0 = e, n.y1 = e + n.value * r, e = n.y1 + b;
				for (let e of n.sourceLinks) e.width = e.value * r;
			}
			e = (_ - e + b) / (t.length + 1);
			for (let n = 0; n < t.length; ++n) {
				let r = t[n];
				r.y0 += e * (n + 1), r.y1 += e * (n + 1);
			}
			H(t);
		}
	}
	function F(t) {
		let n = N(t);
		b = Math.min(y, (_ - h) / (e(n, (e) => e.length) - 1)), P(n);
		for (let e = 0; e < D; ++e) {
			let t = .99 ** e, r = Math.max(1 - t, (e + 1) / D);
			L(n, t, r), I(n, t, r);
		}
	}
	function I(e, t, n) {
		for (let r = 1, i = e.length; r < i; ++r) {
			let i = e[r];
			for (let e of i) {
				let n = 0, r = 0;
				for (let { source: t, value: i } of e.targetLinks) {
					let a = i * (e.layer - t.layer);
					n += U(t, e) * a, r += a;
				}
				if (!(r > 0)) continue;
				let i = (n / r - e.y0) * t;
				e.y0 += i, e.y1 += i, V(e);
			}
			C === void 0 && i.sort(s), R(i, n);
		}
	}
	function L(e, t, n) {
		for (let r = e.length - 2; r >= 0; --r) {
			let i = e[r];
			for (let e of i) {
				let n = 0, r = 0;
				for (let { target: t, value: i } of e.sourceLinks) {
					let a = i * (t.layer - e.layer);
					n += W(e, t) * a, r += a;
				}
				if (!(r > 0)) continue;
				let i = (n / r - e.y0) * t;
				e.y0 += i, e.y1 += i, V(e);
			}
			C === void 0 && i.sort(s), R(i, n);
		}
	}
	function R(e, t) {
		let n = e.length >> 1, r = e[n];
		B(e, r.y0 - b, n - 1, t), z(e, r.y1 + b, n + 1, t), B(e, _, e.length - 1, t), z(e, h, 0, t);
	}
	function z(e, t, n, r) {
		for (; n < e.length; ++n) {
			let i = e[n], a = (t - i.y0) * r;
			a > 1e-6 && (i.y0 += a, i.y1 += a), t = i.y1 + b;
		}
	}
	function B(e, t, n, r) {
		for (; n >= 0; --n) {
			let i = e[n], a = (i.y1 - t) * r;
			a > 1e-6 && (i.y0 -= a, i.y1 -= a), t = i.y0 - b;
		}
	}
	function V({ sourceLinks: e, targetLinks: t }) {
		if (w === void 0) {
			for (let { source: { sourceLinks: e } } of t) e.sort(o);
			for (let { target: { targetLinks: t } } of e) t.sort(a);
		}
	}
	function H(e) {
		if (w === void 0) for (let { sourceLinks: t, targetLinks: n } of e) t.sort(o), n.sort(a);
	}
	function U(e, t) {
		let n = e.y0 - (e.sourceLinks.length - 1) * b / 2;
		for (let { target: r, width: i } of e.sourceLinks) {
			if (r === t) break;
			n += i + b;
		}
		for (let { source: r, width: i } of t.targetLinks) {
			if (r === e) break;
			n -= i;
		}
		return n;
	}
	function W(e, t) {
		let n = t.y0 - (t.targetLinks.length - 1) * b / 2;
		for (let { source: r, width: i } of t.targetLinks) {
			if (r === e) break;
			n += i + b;
		}
		for (let { target: r, width: i } of e.sourceLinks) {
			if (r === t) break;
			n -= i;
		}
		return n;
	}
	return O;
}
//#endregion
export { m as default };
