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/area/index.ts
var c = .2, l = 2, u = class extends a {
	config;
	constructor(e) {
		super(e), this.config = e, this.render();
	}
	computeDataBounds(e) {
		let t = /* @__PURE__ */ new Set();
		if (this.config.stacked === !0) {
			let n = /* @__PURE__ */ new Map();
			for (let r of e) for (let e of r.data) {
				let r = String(e.x);
				t.add(r);
				let i = n.get(r) ?? 0;
				n.set(r, i + e.y);
			}
			let r = 0;
			for (let e of n.values()) e > r && (r = e);
			return {
				labels: Array.from(t),
				yMin: 0,
				yMax: this.config.yAxis?.max ?? r
			};
		}
		let 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.smooth === !0, y = this.config.showLine !== !1, b = this.config.fillOpacity ?? c, x = this.config.stacked === !0, S = /* @__PURE__ */ new Map(), C = x ? [...a].reverse() : a;
		for (let n of C) {
			if (n.data.length === 0) continue;
			let r = [];
			for (let e of n.data) {
				let t = String(e.x), n = h(t) + _, i = x ? S.get(t) ?? 0 : 0, a = g(i + e.y), o = x ? g(i) : p.y + p.height;
				r.push({
					px: n,
					py: a,
					baseY: o
				});
			}
			if (r.length === 0) continue;
			let i = r[0];
			if (i === void 0) continue;
			if (this.ctx.beginPath(), this.ctx.moveTo(i.px, i.baseY), this.ctx.lineTo(i.px, i.py), v && 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];
			if (a !== void 0 && this.ctx.lineTo(a.px, a.baseY), x) for (let e = r.length - 1; e >= 0; e--) {
				let t = r[e];
				t !== void 0 && this.ctx.lineTo(t.px, t.baseY);
			}
			if (this.ctx.closePath(), this.ctx.fillStyle = t(n.color, b), this.ctx.fill(), y && r.length > 1) {
				if (this.ctx.beginPath(), this.ctx.moveTo(i.px, i.py), v && 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 = l, this.ctx.lineJoin = "round", this.ctx.lineCap = "round", this.ctx.stroke();
			}
			if (x) for (let e of n.data) {
				let t = String(e.x), n = S.get(t) ?? 0;
				S.set(t, n + e.y);
			}
		}
		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 AreaChart };

//# sourceMappingURL=index.js.map