import { BaseChart as e } from "../base.js";
import { clamp as t, defaultColors as n, fitText as r, formatNumber as i } from "../utils.js";
//#region src/charts/gauge/index.ts
var a = Math.PI / 180, o = "ui-sans-serif, system-ui, -apple-system, sans-serif", s = `500 11px ${o}`, c = .18, l = .08, u = .3, d = .25, f = {
	track: `rgba(148, 163, 184, ${String(c)})`,
	trackInner: `rgba(148, 163, 184, ${String(l)})`,
	foreground: "#0f172a",
	muted: "#64748b",
	needle: "#0f172a",
	needleSoft: `rgba(15, 23, 42, ${String(u)})`,
	fill: "#3b82f6",
	shadow: `rgba(15, 23, 42, ${String(d)})`
}, p = n()[0] ?? f.fill, m = 32, h = 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.normalizeAngles(), 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.startAngle !== void 0 || e.endAngle !== void 0) && this.normalizeAngles(), e.showValue !== void 0 && (this.showValue = e.showValue), this.render();
	}
	normalizeAngles() {
		for (let e = 0; e < m && this.endAngle <= this.startAngle; e++) this.endAngle += 360;
	}
	resolvePalette() {
		let e = this.container;
		if (!(e instanceof HTMLElement) || !e.isConnected) return f;
		let t = e.ownerDocument.defaultView?.getComputedStyle(e);
		if (t === void 0) return f;
		let n = /^var\(\s*(--[\w-]+)\s*(?:,\s*([^)]+))?\)\s*$/, r = (e, i = 0) => {
			if (i >= 5) return "";
			let a = t.getPropertyValue(e).trim();
			if (a === "") return "";
			let o = n.exec(a);
			if (o !== null) {
				let e = o[1], t = o[2]?.trim() ?? "";
				if (e !== void 0) {
					let t = r(e, i + 1);
					if (t !== "") return t;
				}
				return t;
			}
			return a;
		}, i = (e, t, n = 1) => {
			let i = r(e);
			return i === "" ? t : i.startsWith("#") || i.startsWith("rgb(") || i.startsWith("rgba(") || i.startsWith("hsl(") || i.startsWith("hsla(") || i.startsWith("oklch(") || i.startsWith("oklab(") || i.startsWith("color(") ? i : n === 1 ? `hsl(${i})` : `hsl(${i} / ${n.toString()})`;
		}, a = i("--foreground", f.foreground);
		return {
			track: i("--muted-foreground", f.track, c),
			trackInner: i("--muted-foreground", f.trackInner, l),
			foreground: a,
			muted: i("--muted-foreground", f.muted),
			needle: a,
			needleSoft: i("--foreground", f.needleSoft, u),
			fill: p,
			shadow: i("--foreground", f.shadow, d)
		};
	}
	render() {
		this.clear();
		let e = this.resolvePalette(), t = this.plotArea, n = t.x + t.width / 2, r = t.y + t.height * .58, i = Math.min(t.width / 2, t.height * .55) - this.arcWidth / 2, o = this.startAngle * a, s = this.endAngle * a, c = s - o;
		this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(n, r, i, o, s), this.ctx.strokeStyle = e.trackInner, this.ctx.lineWidth = this.arcWidth + 4, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore(), this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(n, r, i, o, s), this.ctx.strokeStyle = e.track, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore(), this.thresholds.length > 0 ? this.drawThresholdArcs(n, r, i, o, c, e) : this.drawFillArc(n, r, i, o, c, e), this.drawNeedle(n, r, i, o, c, e), this.showValue && this.drawCenterText(n, r, i, e), this.drawMinMaxLabels(n, r, i, o, s, e);
	}
	destroy() {
		super.destroy();
	}
	drawThresholdArcs(e, n, 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 = i + l * a, d = [], f = 0;
		for (let e of s) {
			let n = t((e.value - this.min) / c, 0, 1), r = i + f * a, o = i + n * a, s = Math.min(o, u);
			s > r && d.push({
				start: r,
				end: s,
				color: e.color
			}), f = n;
		}
		if (f < l) {
			let e = s[s.length - 1]?.color ?? o.fill, t = i + f * a;
			d.push({
				start: t,
				end: u,
				color: e
			});
		}
		if (d.length === 0) return;
		let p = d.length - 1;
		if (p === 0) {
			let t = d[0];
			t !== void 0 && this.drawCapArc(e, n, r, t.start, t.end, t.color, o);
			return;
		}
		for (let t of d) this.ctx.save(), this.ctx.shadowColor = o.shadow, this.ctx.shadowBlur = 6, this.ctx.shadowOffsetY = 1, this.ctx.beginPath(), this.ctx.arc(e, n, r, t.start, t.end), this.ctx.strokeStyle = t.color, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "butt", this.ctx.stroke(), this.ctx.restore();
		let m = d[0], h = d[p];
		if (m !== void 0) {
			let t = Math.min(.001, m.end - m.start);
			t > 0 && this.drawCapArc(e, n, r, m.start, m.start + t, m.color, o);
		}
		if (h !== void 0) {
			let t = Math.min(.001, h.end - h.start);
			t > 0 && this.drawCapArc(e, n, r, h.end - t, h.end, h.color, o);
		}
	}
	drawCapArc(e, t, n, r, i, a, o) {
		this.ctx.save(), this.ctx.shadowColor = o.shadow, this.ctx.shadowBlur = 6, this.ctx.shadowOffsetY = 1, this.ctx.beginPath(), this.ctx.arc(e, t, n, r, i), this.ctx.strokeStyle = a, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore();
	}
	drawFillArc(e, n, r, i, a, o) {
		let s = this.max - this.min;
		if (s <= 0) return;
		let c = i + (t(this.value, this.min, this.max) - this.min) / s * a;
		this.ctx.save(), this.ctx.shadowColor = o.shadow, this.ctx.shadowBlur = 8, this.ctx.shadowOffsetY = 2, this.ctx.beginPath(), this.ctx.arc(e, n, r, i, c), this.ctx.strokeStyle = o.fill, this.ctx.lineWidth = this.arcWidth, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore();
	}
	drawNeedle(e, n, r, i, a, o) {
		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 = Math.max(4, r - this.arcWidth - 2), u = e + Math.cos(c) * l, d = n + Math.sin(c) * l;
		this.ctx.save(), this.ctx.beginPath(), this.ctx.moveTo(e, n), this.ctx.lineTo(u, d), this.ctx.strokeStyle = o.needleSoft, this.ctx.lineWidth = 3, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.beginPath(), this.ctx.moveTo(e, n), this.ctx.lineTo(u, d), this.ctx.strokeStyle = o.needle, this.ctx.lineWidth = 1.5, this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.beginPath(), this.ctx.arc(e, n, 6, 0, Math.PI * 2), this.ctx.fillStyle = o.needle, this.ctx.fill(), this.ctx.beginPath(), this.ctx.arc(e, n, 2.5, 0, Math.PI * 2), this.ctx.fillStyle = o.fill, this.ctx.fill(), this.ctx.restore();
	}
	drawCenterText(e, n, a, s) {
		this.ctx.save(), this.ctx.textAlign = "center", this.ctx.textBaseline = "middle";
		let c = t(Math.round(a * .42), 13, 32), l = t(Math.round(a * .16), 9, 13), u = c / 32, d = this.label === void 0 ? n - Math.round(4 * u) : n - Math.round(14 * u), f = n + Math.round(16 * u), p = this.unit === void 0 ? i(this.value) : `${i(this.value)}${this.unit}`;
		if (this.ctx.fillStyle = s.foreground, this.ctx.font = `700 ${c}px ${o}`, this.ctx.fillText(p, e, d), this.label !== void 0) {
			this.ctx.fillStyle = s.muted, this.ctx.font = `500 ${l}px ${o}`;
			let t = r(this.label, a * 1.7, (e) => this.ctx.measureText(e).width);
			this.ctx.fillText(t, e, f);
		}
		this.ctx.restore();
	}
	drawMinMaxLabels(e, t, n, r, a, o) {
		this.ctx.save(), this.ctx.fillStyle = o.muted, this.ctx.font = s, this.ctx.textAlign = "center", this.ctx.textBaseline = "middle";
		let c = n + this.arcWidth / 2 + 14, l = e + Math.cos(r) * c, u = t + Math.sin(r) * c;
		this.ctx.fillText(i(this.min), l, u);
		let d = e + Math.cos(a) * c, f = t + Math.sin(a) * c;
		this.ctx.fillText(i(this.max), d, f), this.ctx.restore();
	}
};
//#endregion
export { h as GaugeChart };

//# sourceMappingURL=index.js.map