import { t as e } from "../../interactive-C6OMqZhG.js";
import { defaultColors as t, formatNumber as n, hexToRgba as r } from "../utils.js";
//#region src/charts/radar/index.ts
var i = Math.PI * 2, a = "#e2e8f0", o = "#cbd5e1", s = "#334155", c = "11px system-ui, sans-serif", l = 3.5, u = 14, d = class extends e {
	labels;
	datasets;
	colors;
	levels;
	showDots;
	fillOpacity;
	maxValue;
	activeDatasets;
	hoveredDataset = -1;
	hoveredAxis = -1;
	constructor(e) {
		super(e), this.labels = e.labels, this.datasets = e.datasets, this.colors = t(), this.levels = e.levels ?? 5, this.showDots = e.showDots ?? !0, this.fillOpacity = e.fillOpacity ?? .2, this.maxValue = e.max, this.activeDatasets = e.datasets.map(() => !0), this.legend !== null && this.legend.onToggle((e, t) => {
			e < this.activeDatasets.length && (this.activeDatasets[e] = t), this.render();
		}), this.render();
	}
	update(e) {
		this.datasets = e, this.activeDatasets = e.map(() => !0), this.render();
	}
	render() {
		this.clear();
		let e = this.plotArea, t = e.x + e.width / 2, n = e.y + e.height / 2, r = Math.min(e.width, e.height) / 2 - u - 4, a = this.labels.length;
		if (a < 3) return;
		let o = this.computeMax();
		if (o <= 0) return;
		let s = i / a;
		this.drawLevels(t, n, r, a, s), this.drawSpokes(t, n, r, a, s), this.drawLabels(t, n, r, a, s);
		for (let e = 0; e < this.datasets.length; e++) {
			let i = this.datasets[e];
			if (i === void 0 || !(this.activeDatasets[e] ?? !0)) continue;
			let c = i.color ?? this.colors[e % this.colors.length] ?? "#3b82f6", l = i.fillOpacity ?? this.fillOpacity, u = e === this.hoveredDataset;
			this.drawDataset(t, n, r, o, a, s, i, c, l, u);
		}
		this.updateLegend();
	}
	computeMax() {
		if (this.maxValue !== void 0) return this.maxValue;
		let e = 0;
		for (let t = 0; t < this.datasets.length; t++) {
			let n = this.datasets[t];
			if (n !== void 0 && (this.activeDatasets[t] ?? !0)) for (let t of n.values) t > e && (e = t);
		}
		return e > 0 ? e : 1;
	}
	drawLevels(e, t, n, r, i) {
		this.ctx.save(), this.ctx.strokeStyle = a, this.ctx.lineWidth = .75;
		for (let a = 1; a <= this.levels; a++) {
			let o = n / this.levels * a;
			this.ctx.beginPath();
			for (let n = 0; n <= r; n++) {
				let r = n * i - Math.PI / 2, a = e + Math.cos(r) * o, s = t + Math.sin(r) * o;
				n === 0 ? this.ctx.moveTo(a, s) : this.ctx.lineTo(a, s);
			}
			this.ctx.closePath(), this.ctx.stroke();
		}
		this.ctx.restore();
	}
	drawSpokes(e, t, n, r, i) {
		this.ctx.save(), this.ctx.strokeStyle = o, this.ctx.lineWidth = .75;
		for (let a = 0; a < r; a++) {
			let r = a * i - Math.PI / 2;
			this.ctx.beginPath(), this.ctx.moveTo(e, t), this.ctx.lineTo(e + Math.cos(r) * n, t + Math.sin(r) * n), this.ctx.stroke();
		}
		this.ctx.restore();
	}
	drawLabels(e, t, n, r, i) {
		this.ctx.save(), this.ctx.fillStyle = s, this.ctx.font = c, this.ctx.textBaseline = "middle";
		for (let a = 0; a < r; a++) {
			let r = this.labels[a];
			if (r === void 0) continue;
			let o = a * i - Math.PI / 2, s = e + Math.cos(o) * (n + u), c = t + Math.sin(o) * (n + u), l = Math.cos(o);
			Math.abs(l) < .01 ? this.ctx.textAlign = "center" : l > 0 ? this.ctx.textAlign = "left" : this.ctx.textAlign = "right", this.ctx.fillText(r, s, c);
		}
		this.ctx.restore();
	}
	drawDataset(e, t, n, a, o, s, c, u, d, f) {
		let p = [];
		for (let r = 0; r < o; r++) {
			let i = (c.values[r] ?? 0) / a, o = r * s - Math.PI / 2, l = e + Math.cos(o) * n * i, u = t + Math.sin(o) * n * i;
			p.push({
				x: l,
				y: u
			});
		}
		if (p.length === 0) return;
		this.ctx.save(), this.ctx.beginPath();
		let m = p[0];
		m !== void 0 && this.ctx.moveTo(m.x, m.y);
		for (let e = 1; e < p.length; e++) {
			let t = p[e];
			t !== void 0 && this.ctx.lineTo(t.x, t.y);
		}
		if (this.ctx.closePath(), this.ctx.fillStyle = r(u, f ? d + .15 : d), this.ctx.fill(), this.ctx.strokeStyle = u, this.ctx.lineWidth = f ? 2.5 : 1.5, this.ctx.stroke(), this.ctx.restore(), this.showDots) {
			this.ctx.save();
			for (let e of p) this.ctx.beginPath(), this.ctx.arc(e.x, e.y, l, 0, i), this.ctx.fillStyle = u, this.ctx.fill(), this.ctx.strokeStyle = "#ffffff", this.ctx.lineWidth = 1, this.ctx.stroke();
			this.ctx.restore();
		}
	}
	updateLegend() {
		if (this.legend === null) return;
		let e = [];
		for (let t = 0; t < this.datasets.length; t++) {
			let n = this.datasets[t];
			if (n === void 0) continue;
			let r = n.color ?? this.colors[t % this.colors.length] ?? "#3b82f6", i = this.activeDatasets[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, a = e.clientY - t.top, o = this.plotArea, s = o.x + o.width / 2, c = o.y + o.height / 2, l = Math.min(o.width, o.height) / 2 - u - 4, d = this.labels.length;
		if (d < 3) return;
		let f = this.computeMax();
		if (f <= 0) return;
		let p = i / d, m = -1, h = Infinity;
		for (let e = 0; e < d; e++) {
			let t = e * p - Math.PI / 2, n = s + Math.cos(t) * l, i = c + Math.sin(t) * l, o = Math.sqrt((r - n) ** 2 + (a - i) ** 2);
			o < h && (h = o, m = e);
		}
		let g = -1, _ = 30;
		for (let e = 0; e < this.datasets.length; e++) {
			let t = this.datasets[e];
			if (t !== void 0 && (this.activeDatasets[e] ?? !0)) for (let n = 0; n < d; n++) {
				let i = (t.values[n] ?? 0) / f, o = n * p - Math.PI / 2, u = s + Math.cos(o) * l * i, d = c + Math.sin(o) * l * i, m = Math.sqrt((r - u) ** 2 + (a - d) ** 2);
				m < _ && (_ = m, g = e);
			}
		}
		let v = g !== this.hoveredDataset || m !== this.hoveredAxis;
		if (this.hoveredDataset = g, this.hoveredAxis = m, v && this.render(), g !== -1 && m !== -1 && this.tooltip !== null) {
			let e = this.datasets[g], t = this.labels[m];
			if (e !== void 0 && t !== void 0) {
				let i = e.values[m] ?? 0, o = `<strong>${e.label}</strong><br/>${t}: ${n(i)}`;
				this.tooltip.show(r, a, o);
			}
		} else this.tooltip !== null && this.tooltip.hide();
	}
	handleMouseLeave() {
		(this.hoveredDataset !== -1 || this.hoveredAxis !== -1) && (this.hoveredDataset = -1, this.hoveredAxis = -1, this.tooltip !== null && this.tooltip.hide(), this.render());
	}
};
//#endregion
export { d as RadarChart };

//# sourceMappingURL=index.js.map