import { drawFilledBar as e, hitTestRect 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/bar/index.ts
var c = .1, l = 0, u = class extends a {
	config;
	barRects;
	hoveredBar;
	constructor(e) {
		super(e), this.config = e, this.barRects = [], this.hoveredBar = null, 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 = 0, r = 0;
		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 {
			labels: Array.from(t),
			yMin: this.config.yAxis?.min ?? n,
			yMax: this.config.yAxis?.max ?? r
		};
	}
	render() {
		this.clear(), this.barRects = [];
		let e = this.getVisibleSeries(), { labels: t, yMin: r, yMax: i } = this.computeDataBounds(e), a = this.plotArea, o = this.config.horizontal === !0, s = this.config.stacked === !0, u = n(r, i, this.config.yAxis?.tickCount ?? 5), d = this.config.barRadius ?? l, f = this.config.barGap ?? c;
		o ? this.renderHorizontal(e, t, u, a, s, f, d) : this.renderVertical(e, t, u, a, s, f, d), this.updateSeriesLegend();
	}
	renderVertical(t, n, a, c, l, u, d) {
		o(this.ctx, c, n, this.config.xAxis), s(this.ctx, c, a.min, a.max, this.config.yAxis);
		let f = r(n, [c.x, c.x + c.width], 4), p = i([a.min, a.max], [c.y + c.height, c.y]), m = f.bandwidth(), h = t.length, g = p(0);
		if (l) for (let r of n) {
			let n = 0;
			for (let i of t) {
				let t = i.data.find((e) => String(e.x) === r)?.y ?? 0, a = p(n), o = p(n + t), s = {
					x: f(r),
					y: o,
					width: m,
					height: a - o,
					color: i.color,
					seriesName: i.name,
					label: r,
					value: t
				};
				this.barRects.push(s), e(this.ctx, s.x, s.y, s.width, s.height, d, s.color, this.isBarHovered(s)), n += t;
			}
		}
		else {
			let r = m * (1 - u), i = h > 0 ? r / h : r, a = (m - r) / 2;
			for (let r of n) for (let n = 0; n < t.length; n++) {
				let o = t[n];
				if (o === void 0) continue;
				let s = o.data.find((e) => String(e.x) === r)?.y ?? 0, c = {
					x: f(r) + a + n * i,
					y: s >= 0 ? p(s) : g,
					width: i,
					height: Math.abs(p(s) - g),
					color: o.color,
					seriesName: o.name,
					label: r,
					value: s
				};
				this.barRects.push(c), e(this.ctx, c.x, c.y, c.width, c.height, d, c.color, this.isBarHovered(c));
			}
		}
	}
	renderHorizontal(t, n, a, c, l, u, d) {
		s(this.ctx, c, a.min, a.max, this.config.xAxis), o(this.ctx, c, n, this.config.yAxis);
		let f = r(n, [c.y, c.y + c.height], 4), p = i([a.min, a.max], [c.x, c.x + c.width]), m = f.bandwidth(), h = t.length, g = p(0);
		if (l) for (let r of n) {
			let n = 0;
			for (let i of t) {
				let t = i.data.find((e) => String(e.x) === r)?.y ?? 0, a = p(n), o = p(n + t), s = {
					x: a,
					y: f(r),
					width: o - a,
					height: m,
					color: i.color,
					seriesName: i.name,
					label: r,
					value: t
				};
				this.barRects.push(s), e(this.ctx, s.x, s.y, s.width, s.height, d, s.color, this.isBarHovered(s)), n += t;
			}
		}
		else {
			let r = m * (1 - u), i = h > 0 ? r / h : r, a = (m - r) / 2;
			for (let r of n) for (let n = 0; n < t.length; n++) {
				let o = t[n];
				if (o === void 0) continue;
				let s = o.data.find((e) => String(e.x) === r)?.y ?? 0, c = s >= 0 ? g : p(s), l = s >= 0 ? p(s) : g, u = {
					x: c,
					y: f(r) + a + n * i,
					width: l - c,
					height: i,
					color: o.color,
					seriesName: o.name,
					label: r,
					value: s
				};
				this.barRects.push(u), e(this.ctx, u.x, u.y, u.width, u.height, d, u.color, this.isBarHovered(u));
			}
		}
	}
	isBarHovered(e) {
		return this.hoveredBar !== null && this.hoveredBar.x === e.x && this.hoveredBar.y === e.y && this.hoveredBar.width === e.width && this.hoveredBar.height === e.height;
	}
	handleMouseMove(e) {
		let n = this.canvas.getBoundingClientRect(), r = e.clientX - n.left, i = e.clientY - n.top, a = null;
		for (let e of this.barRects) if (t(r, i, e)) {
			a = e;
			break;
		}
		let o = this.hoveredBar;
		if (this.hoveredBar = a, a !== null && this.tooltip !== null) {
			let e;
			e = this.config.tooltip?.format === void 0 ? `${a.seriesName}: ${String(a.value)}` : this.config.tooltip.format(a.value, a.label, a.seriesName), this.tooltip.show(a.x + a.width / 2, a.y, e);
		} else this.tooltip !== null && this.tooltip.hide();
		a !== o && this.render();
	}
	handleMouseLeave() {
		this.hoveredBar !== null && (this.hoveredBar = null, this.render()), this.tooltip !== null && this.tooltip.hide();
	}
	update(e) {
		this.series = this.resolveSeries(e), this.render();
	}
};
//#endregion
export { u as BarChart };

//# sourceMappingURL=index.js.map