import { drawSmoothPath as e, hexToRgba as t, niceScale as n, scaleBand as r, scaleLinear as i } from "../utils.js";
import { t as a } from "../../series-DMc2ZYyj.js";
import { drawXAxis as o, drawYAxis as s } from "../axis.js";
//#region src/charts/line/index.ts
var c = 2, l = 4, u = class extends a {
	config;
	constructor(e) {
		super(e), this.config = e, this.render();
	}
	computeDataBounds(e) {
		let t = /* @__PURE__ */ new Set(), n = Infinity, r = -Infinity;
		for (let i of e) for (let e of i.data) t.add(String(e.x)), e.y < n && (n = e.y), e.y > r && (r = e.y);
		return n === Infinity && (n = 0, r = 1), {
			labels: Array.from(t),
			yMin: this.config.yAxis?.min ?? n,
			yMax: this.config.yAxis?.max ?? r
		};
	}
	render() {
		this.clear();
		let a = this.getVisibleSeries(), { labels: u, yMin: d, yMax: f } = this.computeDataBounds(a), p = this.plotArea, m = n(d, f, this.config.yAxis?.tickCount ?? 5);
		o(this.ctx, p, u, this.config.xAxis), s(this.ctx, p, m.min, m.max, this.config.yAxis);
		let h = r(u, [p.x, p.x + p.width], 0), g = i([m.min, m.max], [p.y + p.height, p.y]), _ = h.bandwidth() / 2, v = this.config.lineWidth ?? c, y = this.config.smooth === !0, b = this.config.showDots !== !1, x = this.config.fillArea === !0;
		for (let n of a) {
			if (n.data.length === 0) continue;
			let r = [];
			for (let e of n.data) {
				let t = h(String(e.x)) + _, n = g(e.y);
				r.push({
					px: t,
					py: n
				});
			}
			if (x && r.length > 1) {
				this.ctx.beginPath();
				let i = r[0];
				if (i !== void 0) {
					if (this.ctx.moveTo(i.px, i.py), y && r.length > 2) e(this.ctx, r);
					else for (let e = 1; e < r.length; e++) {
						let t = r[e];
						t !== void 0 && this.ctx.lineTo(t.px, t.py);
					}
					let a = r[r.length - 1];
					a !== void 0 && this.ctx.lineTo(a.px, p.y + p.height), this.ctx.lineTo(i.px, p.y + p.height), this.ctx.closePath(), this.ctx.fillStyle = t(n.color, .15), this.ctx.fill();
				}
			}
			if (r.length > 1) {
				this.ctx.beginPath();
				let t = r[0];
				if (t !== void 0) if (this.ctx.moveTo(t.px, t.py), y && r.length > 2) e(this.ctx, r);
				else for (let e = 1; e < r.length; e++) {
					let t = r[e];
					t !== void 0 && this.ctx.lineTo(t.px, t.py);
				}
				this.ctx.strokeStyle = n.color, this.ctx.lineWidth = v, this.ctx.lineJoin = "round", this.ctx.lineCap = "round", this.ctx.stroke();
			}
			if (b) for (let e of r) this.ctx.beginPath(), this.ctx.arc(e.px, e.py, l, 0, Math.PI * 2), this.ctx.fillStyle = n.color, this.ctx.fill(), this.ctx.strokeStyle = "#ffffff", this.ctx.lineWidth = 1.5, this.ctx.stroke();
		}
		this.updateSeriesLegend();
	}
	handleMouseMove(e) {
		if (this.tooltip === null) return;
		let t = this.canvas.getBoundingClientRect(), a = e.clientX - t.left, o = e.clientY - t.top, s = this.plotArea;
		if (a < s.x || a > s.x + s.width || o < s.y || o > s.y + s.height) {
			this.tooltip.hide();
			return;
		}
		let c = this.getVisibleSeries(), { labels: l, yMin: u, yMax: d } = this.computeDataBounds(c), f = n(u, d, this.config.yAxis?.tickCount ?? 5), p = r(l, [s.x, s.x + s.width], 0), m = i([f.min, f.max], [s.y + s.height, s.y]), h = p.bandwidth() / 2, g = Infinity, _ = "", v = 0, y = 0;
		for (let e of c) for (let t of e.data) {
			let n = p(String(t.x)) + h, r = m(t.y), i = Math.sqrt((a - n) ** 2 + (o - r) ** 2);
			i < g && (g = i, v = n, y = r, _ = this.config.tooltip?.format === void 0 ? `${e.name}: ${String(t.y)}` : this.config.tooltip.format(t.y, String(t.x), e.name));
		}
		g < 30 ? this.tooltip.show(v, y, _) : this.tooltip.hide();
	}
	handleMouseLeave() {
		this.tooltip !== null && this.tooltip.hide();
	}
	update(e) {
		this.series = this.resolveSeries(e), this.render();
	}
};
//#endregion
export { u as LineChart };

//# sourceMappingURL=index.js.map