import { t as e } from "../../interactive-C6OMqZhG.js";
import { drawFilledBar as t, hitTestRect as n, niceScale as r, scaleBand as i, scaleLinear as a } from "../utils.js";
import { drawXAxis as o, drawYAxis as s } from "../axis.js";
//#region src/charts/waterfall/index.ts
var c = "#22c55e", l = "#ef4444", u = "#3b82f6", d = 0, f = "#94a3b8", p = class extends e {
	items;
	config;
	computedBars;
	hoveredBar;
	constructor(e) {
		super({
			...e,
			legend: e.legend ?? { visible: !0 }
		}), this.config = e, this.items = e.items, this.computedBars = [], this.hoveredBar = null, this.render();
	}
	getColors() {
		return {
			increase: this.config.colors?.increase ?? c,
			decrease: this.config.colors?.decrease ?? l,
			total: this.config.colors?.total ?? u
		};
	}
	resolveItemType(e) {
		return e.type === void 0 ? e.value >= 0 ? "increase" : "decrease" : e.type;
	}
	computeWaterfallData() {
		let e = [], t = 0;
		for (let n of this.items) {
			let r = this.resolveItemType(n);
			if (r === "total") e.push({
				label: n.label,
				value: t,
				type: "total",
				startValue: 0,
				endValue: t
			});
			else {
				let i = t;
				t += n.value, e.push({
					label: n.label,
					value: n.value,
					type: r,
					startValue: i,
					endValue: t
				});
			}
		}
		return e;
	}
	render() {
		this.clear(), this.computedBars = [];
		let e = this.computeWaterfallData();
		if (e.length === 0) return;
		let n = this.plotArea, c = e.map((e) => e.label), l = this.getColors(), u = this.config.barRadius ?? d, p = this.config.showConnectors !== !1, m = 0, h = 0;
		for (let t of e) {
			let e = Math.min(t.startValue, t.endValue), n = Math.max(t.startValue, t.endValue);
			e < m && (m = e), n > h && (h = n);
		}
		let g = this.config.yAxis?.tickCount ?? 5, _ = r(this.config.yAxis?.min ?? m, this.config.yAxis?.max ?? h, g);
		o(this.ctx, n, c, this.config.xAxis), s(this.ctx, n, _.min, _.max, this.config.yAxis);
		let v = i(c, [n.x, n.x + n.width], 8), y = a([_.min, _.max], [n.y + n.height, n.y]), b = v.bandwidth();
		for (let n = 0; n < e.length; n++) {
			let r = e[n];
			if (r === void 0) continue;
			let i = v(r.label), a = y(Math.max(r.startValue, r.endValue)), o = y(Math.min(r.startValue, r.endValue)) - a, s;
			s = r.type === "total" ? l.total : r.type === "increase" ? l.increase : l.decrease;
			let c = {
				x: i,
				y: a,
				width: b,
				height: Math.max(o, 1),
				color: s,
				label: r.label,
				value: r.value,
				startValue: r.startValue,
				endValue: r.endValue
			};
			if (this.computedBars.push(c), p && n > 0) {
				let t = e[n - 1];
				if (t !== void 0) {
					let e = v(t.label), n = y(t.endValue);
					this.ctx.beginPath(), this.ctx.moveTo(e + b, n), this.ctx.lineTo(i, n), this.ctx.strokeStyle = f, this.ctx.lineWidth = 1, this.ctx.setLineDash([3, 3]), this.ctx.stroke(), this.ctx.setLineDash([]);
				}
			}
			t(this.ctx, c.x, c.y, c.width, c.height, u, s, this.isBarHovered(c));
		}
		if (this.legend !== null) {
			let e = [
				{
					label: "Increase",
					color: l.increase,
					active: !0
				},
				{
					label: "Decrease",
					color: l.decrease,
					active: !0
				},
				{
					label: "Total",
					color: l.total,
					active: !0
				}
			];
			this.legend.update(e);
		}
	}
	isBarHovered(e) {
		return this.hoveredBar === null ? !1 : this.hoveredBar.x === e.x && this.hoveredBar.y === e.y && this.hoveredBar.label === e.label;
	}
	handleMouseMove(e) {
		let t = this.canvas.getBoundingClientRect(), r = e.clientX - t.left, i = e.clientY - t.top, a = null;
		for (let e of this.computedBars) if (n(r, i, e)) {
			a = e;
			break;
		}
		let o = this.hoveredBar;
		if (this.hoveredBar = a, a !== o && this.render(), a !== null && this.tooltip !== null) {
			let e;
			if (this.config.tooltip?.format !== void 0) e = this.config.tooltip.format(a.value, a.label, a.label);
			else {
				let t = a.value >= 0 ? "+" : "";
				e = `${a.label}: ${t}${String(a.value)}`;
			}
			this.tooltip.show(a.x + a.width / 2, a.y, e);
		} else this.tooltip !== null && this.tooltip.hide();
	}
	handleMouseLeave() {
		this.hoveredBar !== null && (this.hoveredBar = null, this.render()), this.tooltip !== null && this.tooltip.hide();
	}
	update(e) {
		this.items = e, this.render();
	}
};
//#endregion
export { p as WaterfallChart };

//# sourceMappingURL=index.js.map