import { BaseChart as e } from "../base.js";
import { ChartTooltip as t } from "../tooltip.js";
import { formatNumber as n, hexToRgba as r, niceScale as i, scaleLinear as a } from "../utils.js";
import { drawXAxis as o, drawYAxis as s } from "../axis.js";
//#region src/charts/candlestick/index.ts
var c = "#22c55e", l = "#ef4444", u = .2, d = 1, f = class extends e {
	data;
	upColor;
	downColor;
	showVolume;
	tooltipEnabled;
	tooltipFormat;
	tooltip;
	xAxisConfig;
	yAxisConfig;
	handleMouseMove;
	handleMouseLeave;
	hoveredIndex;
	constructor(e) {
		super(e), this.data = e.data, this.upColor = e.upColor ?? c, this.downColor = e.downColor ?? l, this.showVolume = e.showVolume === !0, this.tooltipEnabled = e.tooltip?.enabled !== !1, this.tooltipFormat = e.tooltip?.format ?? null, this.tooltip = null, this.xAxisConfig = e.xAxis, this.yAxisConfig = e.yAxis, this.handleMouseMove = null, this.handleMouseLeave = null, this.hoveredIndex = -1, this.tooltipEnabled && this.setupTooltip(), this.render();
	}
	getPriceRange() {
		if (this.data.length === 0) return {
			min: 0,
			max: 100
		};
		let e = Infinity, t = -Infinity;
		for (let n of this.data) n.low < e && (e = n.low), n.high > t && (t = n.high);
		return e === t ? {
			min: e - 1,
			max: t + 1
		} : {
			min: e,
			max: t
		};
	}
	getVolumeMax() {
		let e = 0;
		for (let t of this.data) {
			let n = t.volume ?? 0;
			n > e && (e = n);
		}
		return e > 0 ? e : 1;
	}
	getPlotAreas() {
		let e = this.plotArea;
		if (this.showVolume) {
			let t = e.height * u, n = e.height - t - 10;
			return {
				priceArea: {
					x: e.x,
					y: e.y,
					width: e.width,
					height: n
				},
				volumeArea: {
					x: e.x,
					y: e.y + n + 10,
					width: e.width,
					height: t
				}
			};
		}
		return {
			priceArea: e,
			volumeArea: null
		};
	}
	setupTooltip() {
		this.tooltip = new t(this.container), this.handleMouseMove = (e) => {
			let t = this.canvas.getBoundingClientRect(), r = e.clientX - t.left, i = e.clientY - t.top, a = this.plotArea;
			if (this.data.length === 0) return;
			let o = a.width / this.data.length, s = Math.floor((r - a.x) / o);
			if (s < 0 || s >= this.data.length || r < a.x || r > a.x + a.width || i < a.y || i > a.y + a.height) {
				this.hoveredIndex !== -1 && (this.hoveredIndex = -1, this.render()), this.tooltip?.hide();
				return;
			}
			s !== this.hoveredIndex && (this.hoveredIndex = s, this.render());
			let c = this.data[s];
			if (c === void 0) {
				this.tooltip?.hide();
				return;
			}
			let l;
			if (this.tooltipFormat !== null) l = this.tooltipFormat(c.close, c.date, "candlestick");
			else {
				let e = [
					`<strong>${c.date}</strong>`,
					`Open: ${n(c.open)}`,
					`High: ${n(c.high)}`,
					`Low: ${n(c.low)}`,
					`Close: ${n(c.close)}`
				];
				c.volume !== void 0 && e.push(`Volume: ${n(c.volume)}`), l = e.join("<br/>");
			}
			this.tooltip?.show(r, i, l);
		}, this.handleMouseLeave = () => {
			this.hoveredIndex !== -1 && (this.hoveredIndex = -1, this.render()), this.tooltip?.hide();
		}, this.canvas.addEventListener("mousemove", this.handleMouseMove), this.canvas.addEventListener("mouseleave", this.handleMouseLeave);
	}
	render() {
		this.clear();
		let { ctx: e } = this, { priceArea: t, volumeArea: n } = this.getPlotAreas(), c = this.data.length;
		if (c === 0) return;
		let l = this.getPriceRange(), u = i(this.yAxisConfig?.min ?? l.min, this.yAxisConfig?.max ?? l.max, this.yAxisConfig?.tickCount ?? 6), f = a([u.min, u.max], [t.y + t.height, t.y]), p = t.width / c, m = Math.max(1, p * .7);
		s(e, t, u.min, u.max, this.yAxisConfig);
		let h = this.data.map((e) => e.date), g = Math.floor(t.width / 60), _ = Math.max(1, Math.ceil(c / g)), v = [];
		for (let e = 0; e < c; e++) {
			let t = h[e];
			e % _ === 0 && t !== void 0 ? v.push(t) : v.push("");
		}
		o(e, t, v, this.xAxisConfig), e.save();
		for (let n = 0; n < c; n++) {
			let i = this.data[n];
			if (i === void 0) continue;
			let a = i.close >= i.open, o = a ? this.upColor : this.downColor, s = n === this.hoveredIndex, c = t.x + p * n + p / 2, l = f(i.high), u = f(i.low), h = f(i.open), g = f(i.close), _ = Math.min(h, g), v = Math.max(1, Math.abs(g - h));
			e.strokeStyle = o, e.lineWidth = d, e.beginPath(), e.moveTo(c, l), e.lineTo(c, u), e.stroke(), e.fillStyle = s ? r(o, .85) : o, e.fillRect(c - m / 2, _, m, v), a && v > 2 && (e.strokeStyle = o, e.lineWidth = 1, e.strokeRect(c - m / 2, _, m, v)), s && (e.strokeStyle = "rgba(100,116,139,0.3)", e.lineWidth = 1, e.setLineDash([2, 2]), e.beginPath(), e.moveTo(c, t.y), e.lineTo(c, t.y + t.height), e.stroke(), e.setLineDash([]));
		}
		if (e.restore(), n !== null) {
			let t = a([0, this.getVolumeMax()], [0, n.height]);
			e.save();
			for (let i = 0; i < c; i++) {
				let a = this.data[i];
				if (a === void 0) continue;
				let o = a.volume ?? 0, s = a.close >= a.open ? this.upColor : this.downColor, c = i === this.hoveredIndex, l = n.x + p * i + p / 2, u = t(o);
				e.fillStyle = c ? r(s, .6) : r(s, .3), e.fillRect(l - m / 2, n.y + n.height - u, m, u);
			}
			e.restore();
		}
	}
	update(e) {
		this.data = e, this.hoveredIndex = -1, this.render();
	}
	destroy() {
		this.handleMouseMove !== null && this.canvas.removeEventListener("mousemove", this.handleMouseMove), this.handleMouseLeave !== null && this.canvas.removeEventListener("mouseleave", this.handleMouseLeave), this.tooltip?.destroy(), super.destroy();
	}
};
//#endregion
export { f as CandlestickChart };

//# sourceMappingURL=index.js.map