import { n as e, t } from "../../utils-CSEWuKHo.js";
//#region src/visualization/timeline/index.ts
function n(e) {
	return e.toLocaleString(void 0, {
		month: "short",
		day: "numeric",
		year: "numeric",
		hour: "2-digit",
		minute: "2-digit"
	});
}
function r(e) {
	return e.toLocaleDateString(void 0, {
		weekday: "long",
		month: "long",
		day: "numeric",
		year: "numeric"
	});
}
function i(e) {
	return e instanceof Date ? e : new Date(e);
}
function a(e) {
	return `${String(e.getFullYear())}-${String(e.getMonth() + 1).padStart(2, "0")}-${String(e.getDate()).padStart(2, "0")}`;
}
var o = class {
	el;
	timelineId;
	orientation;
	showTimestamps;
	dateFormat;
	onEventClick;
	groupByDate;
	events;
	eventHandlers = [];
	constructor(r) {
		this.timelineId = e("cmm-timeline"), this.orientation = r.orientation ?? "vertical", this.showTimestamps = r.showTimestamps !== !1, this.dateFormat = r.dateFormat ?? n, this.onEventClick = r.onEventClick, this.groupByDate = r.groupByDate === !0, this.events = [...r.events], this.el = document.createElement("div"), this.el.id = this.timelineId, this.el.className = this.buildRootClassName(r.className), t(r.container).appendChild(this.el), this.render();
	}
	update(e) {
		this.cleanupHandlers(), this.events = [...e], this.render();
	}
	addEvent(e) {
		this.cleanupHandlers(), this.events.push(e), this.render();
	}
	destroy() {
		this.cleanupHandlers(), this.el.remove();
	}
	render() {
		this.el.replaceChildren(), this.eventHandlers = [];
		let e = [...this.events].sort((e, t) => i(e.timestamp).getTime() - i(t.timestamp).getTime());
		this.orientation === "horizontal" ? this.renderHorizontal(e) : this.renderVertical(e);
	}
	renderVertical(e) {
		let t = document.createElement("div");
		t.className = "cmm-timeline__line", t.setAttribute("aria-hidden", "true"), this.el.appendChild(t), this.groupByDate ? this.renderGroupedVertical(e) : this.renderFlatVertical(e);
	}
	renderFlatVertical(e) {
		for (let t = 0; t < e.length; t++) {
			let n = e[t];
			if (n === void 0) continue;
			let r = t % 2 == 0 ? "left" : "right", i = this.createVerticalEvent(n, r);
			this.el.appendChild(i);
		}
	}
	renderGroupedVertical(e) {
		let t = /* @__PURE__ */ new Map(), n = [];
		for (let r of e) {
			let e = a(i(r.timestamp)), o = t.get(e);
			o === void 0 ? (t.set(e, [r]), n.push(e)) : o.push(r);
		}
		let o = 0;
		for (let e of n) {
			let n = t.get(e);
			if (n === void 0) continue;
			let a = n[0];
			if (a === void 0) continue;
			let s = document.createElement("div");
			s.className = "cmm-timeline__group";
			let c = document.createElement("span");
			c.className = "cmm-timeline__group-label", c.textContent = r(i(a.timestamp)), s.appendChild(c), this.el.appendChild(s);
			for (let e of n) {
				let t = o % 2 == 0 ? "left" : "right", n = this.createVerticalEvent(e, t);
				this.el.appendChild(n), o++;
			}
		}
	}
	createVerticalEvent(e, t) {
		let n = document.createElement("div");
		n.className = `cmm-timeline__event cmm-timeline__event--${t}`, n.setAttribute("data-event-id", e.id);
		let r = document.createElement("div");
		r.className = "cmm-timeline__dot", r.setAttribute("aria-hidden", "true"), e.color !== void 0 && (r.style.background = e.color), n.appendChild(r);
		let i = this.createEventContent(e);
		return n.appendChild(i), n;
	}
	renderHorizontal(e) {
		let t = document.createElement("div");
		t.className = "cmm-timeline__track";
		let n = document.createElement("div");
		n.className = "cmm-timeline__line", n.setAttribute("aria-hidden", "true"), t.appendChild(n);
		for (let n = 0; n < e.length; n++) {
			let r = e[n];
			if (r === void 0) continue;
			let i = n % 2 == 0, a = this.createHorizontalEvent(r, i);
			t.appendChild(a);
		}
		this.el.appendChild(t);
	}
	createHorizontalEvent(e, t) {
		let n = document.createElement("div");
		n.className = `cmm-timeline__event${t ? " cmm-timeline__event--above" : ""}`, n.setAttribute("data-event-id", e.id);
		let r = document.createElement("div");
		r.className = "cmm-timeline__dot", r.setAttribute("aria-hidden", "true"), e.color !== void 0 && (r.style.background = e.color), n.appendChild(r);
		let i = this.createEventContent(e);
		return n.appendChild(i), n;
	}
	createEventContent(e) {
		let t = document.createElement("div");
		t.className = "cmm-timeline__content", t.setAttribute("tabindex", "0"), t.setAttribute("role", "button"), t.setAttribute("aria-label", e.title);
		let n = document.createElement("div");
		if (n.className = "cmm-timeline__title", e.icon !== void 0) {
			let t = document.createElement("span");
			t.className = "cmm-timeline__icon", t.textContent = e.icon, t.setAttribute("aria-hidden", "true"), n.appendChild(t);
		}
		let r = document.createTextNode(e.title);
		if (n.appendChild(r), t.appendChild(n), e.description !== void 0) {
			let n = document.createElement("div");
			n.className = "cmm-timeline__description", n.textContent = e.description, t.appendChild(n);
		}
		if (this.showTimestamps) {
			let n = document.createElement("div");
			n.className = "cmm-timeline__timestamp", n.textContent = this.dateFormat(i(e.timestamp)), t.appendChild(n);
		}
		if (e.metadata !== void 0) {
			let n = document.createElement("div");
			n.className = "cmm-timeline__metadata";
			for (let [t, r] of Object.entries(e.metadata)) {
				let e = document.createElement("span"), i = document.createElement("span");
				i.className = "cmm-timeline__metadata-key", i.textContent = `${t}:`;
				let a = document.createElement("span");
				a.className = "cmm-timeline__metadata-value", a.textContent = r, e.append(i, document.createTextNode(" "), a), n.appendChild(e);
			}
			t.appendChild(n);
		}
		if (this.onEventClick !== void 0) {
			let n = () => {
				this.onEventClick?.(e);
			}, r = (e) => {
				let t = e;
				(t.key === "Enter" || t.key === " ") && (t.preventDefault(), n());
			};
			t.addEventListener("click", n), t.addEventListener("keydown", r), this.eventHandlers.push({
				element: t,
				type: "click",
				handler: n
			}), this.eventHandlers.push({
				element: t,
				type: "keydown",
				handler: r
			});
		}
		return t;
	}
	buildRootClassName(e) {
		let t = ["cmm-timeline", `cmm-timeline--${this.orientation}`];
		return e !== void 0 && t.push(e), t.join(" ");
	}
	cleanupHandlers() {
		for (let e of this.eventHandlers) e.element.removeEventListener(e.type, e.handler);
		this.eventHandlers = [];
	}
};
//#endregion
export { o as Timeline };

//# sourceMappingURL=index.js.map