import { BaseChart as e } from "../base.js";
import { defaultColors as t, hexToRgba as n } from "../utils.js";
//#region src/charts/sparkline/index.ts
var r = 1.5, i = 2, a = 2.5, o = 2, s = class extends e {
	data;
	color;
	fillColor;
	lineWidth;
	showEndDot;
	showMinMax;
	chartType;
	constructor(e) {
		let n = {
			...e,
			padding: {
				top: e.padding?.top ?? i,
				right: e.padding?.right ?? i,
				bottom: e.padding?.bottom ?? i,
				left: e.padding?.left ?? i
			}
		};
		super(n), this.data = e.data, this.color = e.color ?? t()[0] ?? "#3b82f6", this.fillColor = e.fillColor, this.lineWidth = e.lineWidth ?? r, this.showEndDot = e.showEndDot ?? !1, this.showMinMax = e.showMinMax ?? !1, this.chartType = e.type ?? "line", this.render();
	}
	update(e) {
		this.data = e, this.render();
	}
	render() {
		this.clear(), this.data.length !== 0 && (this.chartType === "bar" ? this.renderBars() : this.renderLine());
	}
	renderLine() {
		let e = this.plotArea, t = this.data.length;
		if (t === 0) return;
		let { dataMin: n, dataMax: r } = this.computeMinMax(), i = r - n, s = i === 0 ? 1 : i, c = t > 1 ? e.width / (t - 1) : 0, l = [], u = 0, d = 0, f = Infinity, p = -Infinity;
		for (let r = 0; r < t; r++) {
			let t = this.data[r];
			if (t === void 0) continue;
			let i = e.x + c * r, a = e.y + e.height - (t - n) / s * e.height;
			l.push({
				x: i,
				y: a
			}), t < f && (f = t, u = l.length - 1), t >= p && (p = t, d = l.length - 1);
		}
		if (l.length === 0) return;
		if (this.fillColor !== void 0) {
			this.ctx.save(), this.ctx.beginPath();
			let t = l[0];
			t !== void 0 && (this.ctx.moveTo(t.x, e.y + e.height), this.ctx.lineTo(t.x, t.y));
			for (let e = 1; e < l.length; e++) {
				let t = l[e];
				t !== void 0 && this.ctx.lineTo(t.x, t.y);
			}
			let n = l[l.length - 1];
			n !== void 0 && this.ctx.lineTo(n.x, e.y + e.height), this.ctx.closePath(), this.ctx.fillStyle = this.fillColor, this.ctx.fill(), this.ctx.restore();
		}
		this.ctx.save(), this.ctx.beginPath();
		let m = l[0];
		m !== void 0 && this.ctx.moveTo(m.x, m.y);
		for (let e = 1; e < l.length; e++) {
			let t = l[e];
			t !== void 0 && this.ctx.lineTo(t.x, t.y);
		}
		if (this.ctx.strokeStyle = this.color, this.ctx.lineWidth = this.lineWidth, this.ctx.lineJoin = "round", this.ctx.lineCap = "round", this.ctx.stroke(), this.ctx.restore(), this.showEndDot && l.length > 0) {
			let e = l[l.length - 1];
			e !== void 0 && this.drawDot(e.x, e.y, a, this.color);
		}
		if (this.showMinMax && l.length > 1) {
			let e = l[u], t = l[d];
			e !== void 0 && this.drawDot(e.x, e.y, o, "#ef4444"), t !== void 0 && this.drawDot(t.x, t.y, o, "#22c55e");
		}
	}
	renderBars() {
		let e = this.plotArea, t = this.data.length;
		if (t === 0) return;
		let { dataMin: r, dataMax: i } = this.computeMinMax(), a = i - r, o = a === 0 ? 1 : a, s = Math.max(1, e.width / t - 1), c = e.width / t;
		this.ctx.save();
		for (let i = 0; i < t; i++) {
			let t = this.data[i];
			if (t === void 0) continue;
			let a = (t - r) / o * e.height, l = e.x + c * i + (c - s) / 2, u = e.y + e.height - a;
			this.ctx.fillStyle = n(this.color, .8), this.ctx.fillRect(l, u, s, a);
		}
		this.ctx.restore();
	}
	computeMinMax() {
		let e = Infinity, t = -Infinity;
		for (let n of this.data) n < e && (e = n), n > t && (t = n);
		return isFinite(e) || (e = 0), isFinite(t) || (t = 1), {
			dataMin: e,
			dataMax: t
		};
	}
	drawDot(e, t, n, r) {
		this.ctx.save(), this.ctx.beginPath(), this.ctx.arc(e, t, n, 0, Math.PI * 2), this.ctx.fillStyle = r, this.ctx.fill(), this.ctx.strokeStyle = "#ffffff", this.ctx.lineWidth = 1, this.ctx.stroke(), this.ctx.restore();
	}
};
//#endregion
export { s as SparklineChart };

//# sourceMappingURL=index.js.map