import { BaseChart as e } from "../base.js";
import { clamp as t, defaultColors as n, formatNumber as r } from "../utils.js";
//#region src/charts/gauge/index.ts
var i = Math.PI / 180, a = "#e2e8f0", o = "#1e293b", s = "bold 28px system-ui, sans-serif", c = "14px system-ui, sans-serif", l = "13px system-ui, sans-serif", u = "#64748b", d = class extends e {
	value;
	min;
	max;
	label;
	unit;
	thresholds;
	arcWidth;
	startAngle;
	endAngle;
	showValue;
	constructor(e) {
		super(e), this.value = e.value, this.min = e.min ?? 0, this.max = e.max ?? 100, this.label = e.label, this.unit = e.unit, this.thresholds = e.thresholds ?? [], this.arcWidth = e.arcWidth ?? 20, this.startAngle = e.startAngle ?? 135, this.endAngle = e.endAngle ?? 405, this.showValue = e.showValue ?? !0, this.render();
	}
	updateValue(e) {
		this.value = e, this.render();
	}
	updateConfig(e) {
		e.value !== void 0 && (this.value = e.value), e.min !== void 0 && (this.min = e.min), e.max !== void 0 && (this.max = e.max), e.label !== void 0 && (this.label = e.label), e.unit !== void 0 && (this.unit = e.unit), e.thresholds !== void 0 && (this.thresholds = e.thresholds), e.arcWidth !== void 0 && (this.arcWidth = e.arcWidth), e.startAngle !== void 0 && (this.startAngle = e.startAngle), e.endAngle !== void 0 && (this.endAngle = e.endAngle), e.showValue !== void 0 && (this.showValue = e.showValue), this.render();
	}
	render() {
		this.clear();
		let e = this.plotArea, t = e.x + e.width / 2, n = e.y + e.height * .6, r = Math.min(e.width / 2, e.height * .55) - this.arcWidth / 2, o = this.startAngle * i, s = this.endAngle * i, c = s - o;
		this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(t, n, r, o, s), this.ctx.strokeStyle = a, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore(), this.thresholds.length > 0 ? this.drawThresholdArcs(t, n, r, o, c) : this.drawFillArc(t, n, r, o, c), this.drawNeedle(t, n, r, o, c), this.showValue && this.drawCenterText(t, n);
	}
	destroy() {
		super.destroy();
	}
	drawThresholdArcs(e, r, i, a, o) {
		let s = [...this.thresholds].sort((e, t) => e.value - t.value), c = this.max - this.min;
		if (c <= 0) return;
		let l = (t(this.value, this.min, this.max) - this.min) / c, u = 0;
		for (let n of s) {
			let s = t((n.value - this.min) / c, 0, 1), d = a + u * o, f = a + s * o, p = a + l * o, m = Math.min(f, p);
			m > d && (this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(e, r, i, d, m), this.ctx.strokeStyle = n.color, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "butt", this.ctx.stroke(), this.ctx.restore()), u = s;
		}
		if (u < l) {
			let t = s[s.length - 1]?.color ?? n()[0] ?? "#3b82f6", c = a + u * o, d = a + l * o;
			this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(e, r, i, c, d), this.ctx.strokeStyle = t, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "butt", this.ctx.stroke(), this.ctx.restore();
		}
	}
	drawFillArc(e, r, i, a, o) {
		let s = this.max - this.min;
		if (s <= 0) return;
		let c = a + (t(this.value, this.min, this.max) - this.min) / s * o, l = n()[0] ?? "#3b82f6";
		this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(e, r, i, a, c), this.ctx.strokeStyle = l, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore();
	}
	drawNeedle(e, n, r, i, a) {
		let s = this.max - this.min;
		if (s <= 0) return;
		let c = i + (t(this.value, this.min, this.max) - this.min) / s * a, l = r - this.arcWidth / 2 - 4;
		this.ctx.save(), this.ctx.beginPath(), this.ctx.moveTo(e, n), this.ctx.lineTo(e + Math.cos(c) * l, n + Math.sin(c) * l), this.ctx.strokeStyle = o, this.ctx.lineWidth = 2.5, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.beginPath(), this.ctx.arc(e, n, 5, 0, Math.PI * 2), this.ctx.fillStyle = o, this.ctx.fill(), this.ctx.restore();
	}
	drawCenterText(e, t) {
		this.ctx.save(), this.ctx.textAlign = "center", this.ctx.textBaseline = "middle";
		let n = this.unit === void 0 ? r(this.value) : `${r(this.value)}${this.unit}`;
		this.ctx.fillStyle = o, this.ctx.font = s, this.ctx.fillText(n, e, t + 30), this.label !== void 0 && (this.ctx.fillStyle = u, this.ctx.font = l, this.ctx.fillText(this.label, e, t + 52)), this.ctx.fillStyle = u, this.ctx.font = c;
		let a = this.plotArea, d = Math.min(a.width / 2, a.height * .55) - this.arcWidth / 2, f = this.startAngle * i, p = this.endAngle * i, m = e + Math.cos(f) * (d + this.arcWidth), h = t + Math.sin(f) * (d + this.arcWidth);
		this.ctx.textAlign = "center", this.ctx.fillText(r(this.min), m, h + 14);
		let g = e + Math.cos(p) * (d + this.arcWidth), _ = t + Math.sin(p) * (d + this.arcWidth);
		this.ctx.fillText(r(this.max), g, _ + 14), this.ctx.restore();
	}
};
//#endregion
export { d as GaugeChart };

//# sourceMappingURL=index.js.map