import { t as e } from "../../interactive-C6OMqZhG.js";
import { defaultColors as t, formatNumber as n, hexToRgba as r } from "../utils.js";
//#region src/charts/pie/index.ts
var i = Math.PI / 180, a = "11px system-ui, sans-serif", o = "bold 11px system-ui, sans-serif", s = 6, c = .15, l = class extends e {
	slices;
	resolved = [];
	colors;
	donut;
	donutRatio;
	showLabels;
	showValues;
	startAngle;
	hoveredIndex = -1;
	activeSlices;
	constructor(e) {
		super(e), this.slices = e.slices, this.colors = t(), this.donut = e.donut ?? !1, this.donutRatio = e.donutRatio ?? .6, this.showLabels = e.showLabels ?? !1, this.showValues = e.showValues ?? !1, this.startAngle = e.startAngle ?? -90, this.activeSlices = e.slices.map(() => !0), this.legend !== null && this.legend.onToggle((e, t) => {
			e < this.activeSlices.length && (this.activeSlices[e] = t), this.resolveSlices(), this.render();
		}), this.resolveSlices(), this.render();
	}
	update(e) {
		this.slices = e, this.activeSlices = e.map(() => !0), this.resolveSlices(), this.updateLegend(), this.render();
	}
	render() {
		this.clear();
		let e = this.plotArea, t = e.x + e.width / 2, n = e.y + e.height / 2, l = Math.min(e.width, e.height) / 2, u = this.donut ? l * this.donutRatio : 0;
		for (let e = 0; e < this.resolved.length; e++) {
			let d = this.resolved[e];
			if (d === void 0) continue;
			let f = e === this.hoveredIndex, p = 0, m = 0;
			if (f) {
				let e = d.midAngle * i;
				p = Math.cos(e) * s, m = Math.sin(e) * s;
			}
			let h = t + p, g = n + m;
			if (this.ctx.save(), this.ctx.beginPath(), this.ctx.moveTo(h + Math.cos(d.startAngle * i) * u, g + Math.sin(d.startAngle * i) * u), this.ctx.arc(h, g, l, d.startAngle * i, d.endAngle * i), this.ctx.arc(h, g, u, d.endAngle * i, d.startAngle * i, !0), this.ctx.closePath(), this.ctx.fillStyle = f ? r(d.color, 1 - c) : d.color, this.ctx.fill(), this.ctx.strokeStyle = "#ffffff", this.ctx.lineWidth = 1.5, this.ctx.stroke(), this.ctx.restore(), this.showLabels || this.showValues) {
				let e = u + (l - u) * .65, r = d.midAngle * i, s = t + Math.cos(r) * e, c = n + Math.sin(r) * e;
				this.ctx.save(), this.ctx.textAlign = "center", this.ctx.textBaseline = "middle", this.ctx.fillStyle = "#ffffff", this.showLabels && this.showValues ? (this.ctx.font = a, this.ctx.fillText(d.label, s, c - 7), this.ctx.font = o, this.ctx.fillText(`${d.percentage.toFixed(1)}%`, s, c + 7)) : this.showLabels ? (this.ctx.font = a, this.ctx.fillText(d.label, s, c)) : (this.ctx.font = o, this.ctx.fillText(`${d.percentage.toFixed(1)}%`, s, c)), this.ctx.restore();
			}
		}
		this.updateLegend();
	}
	resolveSlices() {
		let e = this.computeTotal();
		if (e <= 0) {
			this.resolved = [];
			return;
		}
		let t = [], n = this.startAngle;
		for (let r = 0; r < this.slices.length; r++) {
			let i = this.slices[r];
			if (i === void 0 || !(this.activeSlices[r] ?? !0)) continue;
			let a = i.value / e * 100, o = i.value / e * 360, s = i.color ?? this.colors[r % this.colors.length] ?? "#3b82f6";
			t.push({
				label: i.label,
				value: i.value,
				color: s,
				startAngle: n,
				endAngle: n + o,
				midAngle: n + o / 2,
				percentage: a
			}), n += o;
		}
		this.resolved = t;
	}
	computeTotal() {
		let e = 0;
		for (let t = 0; t < this.slices.length; t++) {
			let n = this.slices[t];
			n !== void 0 && (this.activeSlices[t] ?? !0) && (e += n.value);
		}
		return e;
	}
	updateLegend() {
		if (this.legend === null) return;
		let e = [];
		for (let t = 0; t < this.slices.length; t++) {
			let n = this.slices[t];
			if (n === void 0) continue;
			let r = n.color ?? this.colors[t % this.colors.length] ?? "#3b82f6", i = this.activeSlices[t] ?? !0;
			e.push({
				label: n.label,
				color: r,
				active: i
			});
		}
		this.legend.update(e);
	}
	handleMouseMove(e) {
		let t = this.canvas.getBoundingClientRect(), r = e.clientX - t.left, i = e.clientY - t.top, a = this.plotArea, o = a.x + a.width / 2, s = a.y + a.height / 2, c = Math.min(a.width, a.height) / 2, l = this.donut ? c * this.donutRatio : 0, u = r - o, d = i - s, f = Math.sqrt(u * u + d * d);
		if (f < l || f > c) {
			this.hoveredIndex !== -1 && (this.hoveredIndex = -1, this.tooltip !== null && this.tooltip.hide(), this.render());
			return;
		}
		let p = 180 / Math.PI * Math.atan2(d, u), m = this.resolved[0];
		if (m !== void 0) for (; p < m.startAngle;) p += 360;
		let h = -1;
		for (let e = 0; e < this.resolved.length; e++) {
			let t = this.resolved[e];
			if (t === void 0) continue;
			let n = t.startAngle, r = t.endAngle;
			for (; n > p + 360;) n -= 360, r -= 360;
			for (; r < p - 360;) n += 360, r += 360;
			if (p >= n && p < r) {
				h = e;
				break;
			}
		}
		if (h !== this.hoveredIndex && (this.hoveredIndex = h, this.render()), h !== -1 && this.tooltip !== null) {
			let e = this.resolved[h];
			if (e !== void 0) {
				let t = `<strong>${e.label}</strong><br/>${n(e.value)} (${e.percentage.toFixed(1)}%)`;
				this.tooltip.show(r, i, t);
			}
		} else this.tooltip !== null && this.tooltip.hide();
	}
	handleMouseLeave() {
		this.hoveredIndex !== -1 && (this.hoveredIndex = -1, this.tooltip !== null && this.tooltip.hide(), this.render());
	}
};
//#endregion
export { l as PieChart };

//# sourceMappingURL=index.js.map