import { t as e } from "../../sanitize-CDxuPfMB.js";
import { n as t, t as n } from "../../utils-C_mWfcFX.js";
//#region src/visualization/timeline/index.ts
function r(e) {
	return e.toLocaleString(void 0, {
		month: "short",
		day: "numeric",
		year: "numeric",
		hour: "2-digit",
		minute: "2-digit"
	});
}
function i(e) {
	return e.toLocaleDateString(void 0, {
		weekday: "long",
		month: "long",
		day: "numeric",
		year: "numeric"
	});
}
function a(e) {
	return e instanceof Date ? e : new Date(e);
}
function o(e) {
	return `${String(e.getFullYear())}-${String(e.getMonth() + 1).padStart(2, "0")}-${String(e.getDate()).padStart(2, "0")}`;
}
var s = class {
	el;
	timelineId;
	orientation;
	showTimestamps;
	dateFormat;
	onEventClick;
	groupByDate;
	events;
	eventHandlers = [];
	constructor(e) {
		this.timelineId = t("cmm-timeline"), this.orientation = e.orientation ?? "vertical", this.showTimestamps = e.showTimestamps !== !1, this.dateFormat = e.dateFormat ?? r, this.onEventClick = e.onEventClick, this.groupByDate = e.groupByDate === !0, this.events = [...e.events], this.el = document.createElement("div"), this.el.id = this.timelineId, this.el.className = this.buildRootClassName(e.className), n(e.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.innerHTML = "", this.eventHandlers = [];
		let e = [...this.events].sort((e, t) => a(e.timestamp).getTime() - a(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 = o(a(r.timestamp)), i = t.get(e);
			i === void 0 ? (t.set(e, [r]), n.push(e)) : i.push(r);
		}
		let r = 0;
		for (let e of n) {
			let n = t.get(e);
			if (n === void 0) continue;
			let o = n[0];
			if (o === 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 = i(a(o.timestamp)), s.appendChild(c), this.el.appendChild(s);
			for (let e of n) {
				let t = r % 2 == 0 ? "left" : "right", n = this.createVerticalEvent(e, t);
				this.el.appendChild(n), r++;
			}
		}
	}
	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(t) {
		let n = document.createElement("div");
		n.className = "cmm-timeline__content", n.setAttribute("tabindex", "0"), n.setAttribute("role", "button"), n.setAttribute("aria-label", t.title);
		let r = document.createElement("div");
		if (r.className = "cmm-timeline__title", t.icon !== void 0) {
			let e = document.createElement("span");
			e.className = "cmm-timeline__icon", e.textContent = t.icon, e.setAttribute("aria-hidden", "true"), r.appendChild(e);
		}
		let i = document.createTextNode(t.title);
		if (r.appendChild(i), n.appendChild(r), t.description !== void 0) {
			let e = document.createElement("div");
			e.className = "cmm-timeline__description", e.textContent = t.description, n.appendChild(e);
		}
		if (this.showTimestamps) {
			let e = document.createElement("div");
			e.className = "cmm-timeline__timestamp", e.textContent = this.dateFormat(a(t.timestamp)), n.appendChild(e);
		}
		if (t.metadata !== void 0) {
			let r = document.createElement("div");
			r.className = "cmm-timeline__metadata";
			for (let [n, i] of Object.entries(t.metadata)) {
				let t = document.createElement("span");
				t.innerHTML = `<span class="cmm-timeline__metadata-key">${e(n)}:</span> <span class="cmm-timeline__metadata-value">${e(i)}</span>`, r.appendChild(t);
			}
			n.appendChild(r);
		}
		if (this.onEventClick !== void 0) {
			let e = () => {
				this.onEventClick?.(t);
			}, r = (t) => {
				let n = t;
				(n.key === "Enter" || n.key === " ") && (n.preventDefault(), e());
			};
			n.addEventListener("click", e), n.addEventListener("keydown", r), this.eventHandlers.push({
				element: n,
				type: "click",
				handler: e
			}), this.eventHandlers.push({
				element: n,
				type: "keydown",
				handler: r
			});
		}
		return n;
	}
	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 { s as Timeline };

//# sourceMappingURL=index.js.map