import { BaseChart as e } from "../base.js";
import { ChartTooltip as t } from "../tooltip.js";
import { defaultColors as n, formatNumber as r, hexToRgba as i, roundRect as a } from "../utils.js";
//#region src/charts/treemap/index.ts
var o = "11px system-ui, sans-serif", s = "#ffffff", c = 40, l = 20, u = 2, d = class extends e {
	data;
	showLabels;
	cellGap;
	cellRadius;
	tooltipEnabled;
	tooltipFormat;
	tooltip;
	rects;
	colors;
	handleMouseMove;
	handleMouseLeave;
	hoveredIndex;
	constructor(e) {
		super(e), this.data = f(e.data), this.showLabels = e.showLabels !== !1, this.cellGap = e.cellGap ?? u, this.cellRadius = e.cellRadius ?? 0, this.tooltipEnabled = e.tooltip?.enabled !== !1, this.tooltipFormat = e.tooltip?.format ?? null, this.tooltip = null, this.rects = [], this.colors = n(), this.handleMouseMove = null, this.handleMouseLeave = null, this.hoveredIndex = -1, this.tooltipEnabled && this.setupTooltip(), this.render();
	}
	computeLayout() {
		this.rects = [];
		let e = this.plotArea, t = this.data.children;
		if (t === void 0 || t.length === 0) {
			this.rects.push({
				x: e.x,
				y: e.y,
				width: e.width,
				height: e.height,
				node: this.data,
				color: this.data.color ?? this.colors[0] ?? "#3b82f6"
			});
			return;
		}
		this.squarify(t, e.x, e.y, e.width, e.height, 0);
	}
	squarify(e, t, n, r, i, a) {
		if (e.length === 0 || r <= 0 || i <= 0) return;
		let o = e.reduce((e, t) => e + Math.max(0, t.value), 0);
		if (o <= 0) return;
		let s = [...e].sort((e, t) => t.value - e.value);
		this.layoutRow(s, t, n, r, i, o, a);
	}
	layoutRow(e, t, n, r, i, a, o) {
		if (e.length === 0 || a <= 0) return;
		let s = r >= i, c = [], l = 0, u = Infinity, d = 0;
		for (let t = 0; t < e.length; t++) {
			let n = e[t];
			if (n === void 0) continue;
			let o = [...c, n], f = l + Math.max(0, n.value), p = this.worstAspectRatio(o, f, a, s ? i : r, s ? r : i);
			if (p <= u || c.length === 0) c = o, l = f, u = p, d = t + 1;
			else break;
		}
		let f = l / a, p = s ? r * f : i * f, m = 0;
		for (let e = 0; e < c.length; e++) {
			let a = c[e];
			if (a === void 0) continue;
			let u = l > 0 ? Math.max(0, a.value) / l : 0, d = (s ? i : r) * u, f = this.cellGap, h = s ? t + f / 2 : t + m + f / 2, g = s ? n + m + f / 2 : n + f / 2, _ = s ? p - f : d - f, v = s ? d - f : p - f;
			if (_ > 0 && v > 0) {
				let t = a.color ?? this.colors[(o + e) % this.colors.length] ?? "#3b82f6";
				a.children !== void 0 && a.children.length > 0 ? this.squarify(a.children, h, g, _, v, o + e) : this.rects.push({
					x: h,
					y: g,
					width: _,
					height: v,
					node: a,
					color: t
				});
			}
			m += d;
		}
		let h = e.slice(d);
		if (h.length > 0) {
			let e = a - l;
			s ? this.layoutRow(h, t + p, n, r - p, i, e, o + d) : this.layoutRow(h, t, n + p, r, i - p, e, o + d);
		}
	}
	worstAspectRatio(e, t, n, r, i) {
		if (e.length === 0 || n <= 0 || r <= 0 || i <= 0) return Infinity;
		let a = t / n * r * i / r, o = 0;
		for (let t of e) {
			let e = Math.max(0, t.value) / n * r * i;
			if (a <= 0 || e <= 0) continue;
			let s = e / a, c = Math.max(a / s, s / a);
			c > o && (o = c);
		}
		return o;
	}
	setupTooltip() {
		this.tooltip = new t(this.container), this.handleMouseMove = (e) => {
			let t = this.canvas.getBoundingClientRect(), n = e.clientX - t.left, i = e.clientY - t.top, a = -1;
			for (let e = 0; e < this.rects.length; e++) {
				let t = this.rects[e];
				if (t !== void 0 && n >= t.x && n <= t.x + t.width && i >= t.y && i <= t.y + t.height) {
					a = e;
					break;
				}
			}
			if (a !== this.hoveredIndex && (this.hoveredIndex = a, this.render()), a >= 0) {
				let e = this.rects[a];
				if (e !== void 0) {
					let t;
					t = this.tooltipFormat === null ? `<strong>${e.node.label}</strong><br/>Value: ${r(e.node.value)}` : this.tooltipFormat(e.node.value, e.node.label, ""), this.tooltip?.show(n, i, t);
				}
			} else this.tooltip?.hide();
		}, this.handleMouseLeave = () => {
			this.hoveredIndex !== -1 && (this.hoveredIndex = -1, this.render()), this.tooltip?.hide();
		}, this.canvas.addEventListener("mousemove", this.handleMouseMove), this.canvas.addEventListener("mouseleave", this.handleMouseLeave);
	}
	render() {
		this.clear(), this.computeLayout();
		let { ctx: e } = this;
		e.save();
		for (let t = 0; t < this.rects.length; t++) {
			let n = this.rects[t];
			if (n === void 0) continue;
			let u = t === this.hoveredIndex;
			e.fillStyle = u ? i(n.color, .8) : n.color, this.cellRadius > 0 ? (a(e, n.x, n.y, n.width, n.height, this.cellRadius), e.fill()) : e.fillRect(n.x, n.y, n.width, n.height), u && (e.strokeStyle = "#ffffff", e.lineWidth = 2, this.cellRadius > 0 ? (a(e, n.x, n.y, n.width, n.height, this.cellRadius), e.stroke()) : e.strokeRect(n.x, n.y, n.width, n.height)), this.showLabels && n.width >= c && n.height >= l && (e.fillStyle = s, e.font = o, e.textAlign = "left", e.textBaseline = "top", e.fillText(n.node.label, n.x + 4, n.y + 4, n.width - 8), n.height >= l * 2 && (e.fillStyle = "rgba(255,255,255,0.8)", e.fillText(r(n.node.value), n.x + 4, n.y + 18, n.width - 8)));
		}
		e.restore();
	}
	update(e) {
		this.data = e, this.hoveredIndex = -1, this.render();
	}
	destroy() {
		this.handleMouseMove !== null && this.canvas.removeEventListener("mousemove", this.handleMouseMove), this.handleMouseLeave !== null && this.canvas.removeEventListener("mouseleave", this.handleMouseLeave), this.tooltip?.destroy(), super.destroy();
	}
};
function f(e) {
	let t = { ...e };
	return e.children && e.children.length > 0 && (t.children = e.children.map(f), (!Number.isFinite(e.value) || e.value <= 0) && (t.value = t.children.reduce((e, t) => e + Math.max(0, t.value), 0))), t;
}
//#endregion
export { d as TreemapChart };

//# sourceMappingURL=index.js.map