import { i as e } from "../../sanitize-CDxuPfMB.js";
import { n as t, t as n } from "../../utils-C_mWfcFX.js";
//#region src/visualization/kanban/index.ts
function r(e, t, n) {
	let r = document.createElement(e);
	return r.className = t, n !== void 0 && (r.textContent = n), r;
}
var i = new Set([
	"low",
	"medium",
	"high",
	"critical"
]), a = class {
	container;
	config;
	instanceId;
	columns;
	wrapperEl = null;
	dragState = null;
	collapsedState = /* @__PURE__ */ new Map();
	constructor(e) {
		this.container = n(e.container), this.config = e, this.instanceId = t("cmm-kanban"), this.columns = this.cloneColumns(e.columns);
		for (let e of this.columns) e.collapsible === !0 && this.collapsedState.set(e.id, e.collapsed === !0);
		this.render();
	}
	update(e) {
		this.columns = this.cloneColumns(e), this.render();
	}
	addCard(e, t) {
		let n = this.columns.find((t) => t.id === e);
		n !== void 0 && (n.cards.push({ ...t }), this.render());
	}
	removeCard(e) {
		for (let t of this.columns) {
			let n = t.cards.findIndex((t) => t.id === e);
			if (n >= 0) {
				t.cards.splice(n, 1);
				break;
			}
		}
		this.render();
	}
	moveCard(e, t, n) {
		this.performMove(e, "", t, n);
	}
	destroy() {
		this.container.innerHTML = "", this.wrapperEl = null, this.dragState = null;
	}
	cloneColumns(e) {
		return e.map((e) => ({
			...e,
			cards: e.cards.map((e) => ({ ...e }))
		}));
	}
	isAnimationEnabled() {
		let { animations: e } = this.config;
		return e === void 0 ? !0 : typeof e == "boolean" ? e : e.entrance !== !1;
	}
	getDescriptionMaxLength() {
		return this.config.descriptionMaxLength ?? 100;
	}
	getMaxLabels() {
		return this.config.maxLabels ?? 3;
	}
	getMaxAssigneeAvatars() {
		return this.config.maxAssigneeAvatars ?? 3;
	}
	render() {
		this.container.innerHTML = "";
		let e = r("div", "cmm-kanban");
		if (e.id = this.instanceId, this.config.className !== void 0 && e.classList.add(this.config.className), this.config.density === "compact" && e.classList.add("cmm-kanban--compact"), this.config.swimlaneGroups !== void 0 && this.config.swimlaneGroups.length > 0) this.renderWithSwimlanes(e);
		else for (let t of this.columns) e.appendChild(this.renderColumn(t));
		this.wrapperEl = e, this.container.appendChild(e);
	}
	renderWithSwimlanes(e) {
		let t = this.config.swimlaneGroups;
		if (t !== void 0) for (let n of t) {
			let t = r("div", "cmm-kanban__swimlane");
			t.dataset.swimlaneId = n.id;
			let i = 0;
			for (let e of this.columns) {
				let t = e.cards.filter((e) => n.filter(e));
				i += t.length;
			}
			let a = r("div", "cmm-kanban__swimlane-header");
			a.appendChild(r("span", "cmm-kanban__swimlane-title", n.label)), a.appendChild(r("span", "cmm-kanban__swimlane-count", String(i))), t.appendChild(a);
			let o = r("div", "cmm-kanban__swimlane-columns");
			o.style.display = "flex", o.style.gap = "1rem";
			for (let e of this.columns) {
				let t = e.cards.filter((e) => n.filter(e)), r = {
					...e,
					cards: t
				};
				o.appendChild(this.renderColumn(r));
			}
			t.appendChild(o), e.appendChild(t);
		}
	}
	renderColumn(t) {
		let n = r("div", "cmm-kanban__column");
		n.dataset.columnId = t.id;
		let i = this.collapsedState.get(t.id) === !0, a = t.limit !== void 0 && t.cards.length >= t.limit;
		a && n.classList.add("cmm-kanban__column--at-limit"), i && n.classList.add("cmm-kanban__column--collapsed");
		let o = r("div", "cmm-kanban__column-header");
		if (t.color !== void 0 && (o.style.borderTopColor = t.color), t.icon !== void 0) {
			let n = r("span", "cmm-kanban__column-icon");
			e(n, t.icon), o.appendChild(n);
		}
		o.appendChild(r("span", "cmm-kanban__column-title", t.title));
		let s = r("span", "cmm-kanban__column-count", String(t.cards.length));
		if (t.limit !== void 0 && (s.textContent += ` / ${String(t.limit)}`, a && s.classList.add("cmm-kanban__column-count--warning")), o.appendChild(s), t.collapsible === !0) {
			let e = r("button", "cmm-kanban__column-collapse", i ? "+" : "-");
			e.addEventListener("click", () => {
				let e = this.collapsedState.get(t.id) !== !0;
				this.collapsedState.set(t.id, e), this.config.onColumnCollapse?.(t.id, e), this.render();
			}), o.appendChild(e);
		}
		if (t.addButton === !0) {
			let e = r("button", "cmm-kanban__add-btn", "+");
			e.addEventListener("click", () => {
				this.config.onAddCard?.(t.id);
			}), o.appendChild(e);
		}
		if (n.appendChild(o), !i) {
			let e = r("div", "cmm-kanban__card-list");
			if (e.dataset.columnId = t.id, this.isDraggable() && this.setupDropZone(e, t.id), t.cards.length === 0 && t.emptyMessage !== void 0) e.appendChild(r("div", "cmm-kanban__empty-state", t.emptyMessage));
			else for (let n = 0; n < t.cards.length; n++) {
				let r = t.cards[n];
				r !== void 0 && e.appendChild(this.renderCard(r, t.id, n));
			}
			n.appendChild(e);
		}
		return n;
	}
	renderCard(e, t, n) {
		let a = r("div", "cmm-kanban__card");
		return a.dataset.cardId = e.id, e.priority !== void 0 && i.has(e.priority) && a.classList.add(`cmm-kanban__card--${e.priority}`), this.isAnimationEnabled() && (a.classList.add("cmm-kanban__card--enter"), a.style.animationDelay = `${String(n * 50)}ms`), this.isDraggable() && (a.draggable = !0, this.setupDragSource(a, e.id, t)), a.addEventListener("click", () => {
			this.config.onCardClick?.(e);
		}), a.setAttribute("tabindex", "0"), a.setAttribute("role", "listitem"), this.renderIssueRow(a, e), a.appendChild(r("div", "cmm-kanban__card-title", e.title)), this.renderContextChips(a, e), this.renderDescription(a, e), this.renderProgressBar(a, e), this.renderDueDate(a, e), this.renderStats(a, e), this.renderAssignees(a, e), this.renderLabels(a, e), this.renderActions(a, e, t), a;
	}
	renderIssueRow(e, t) {
		if (!(t.issueKey !== void 0 || t.issueType !== void 0 || t.storyPoints !== void 0 || t.rank !== void 0)) return;
		let n = r("div", "cmm-kanban__card-issue");
		if (t.issueKey !== void 0 && n.appendChild(r("span", "cmm-kanban__issue-key", t.issueKey)), t.issueType !== void 0) {
			let e = t.issueTypeIcon ?? "";
			n.appendChild(r("span", "cmm-kanban__issue-type", e + t.issueType));
		}
		t.storyPoints !== void 0 && n.appendChild(r("span", "cmm-kanban__story-points", String(t.storyPoints))), t.rank !== void 0 && n.appendChild(r("span", "cmm-kanban__rank", String(t.rank))), e.appendChild(n);
	}
	renderContextChips(e, t) {
		if (!(t.epicName !== void 0 || t.sprintName !== void 0 || t.parentId !== void 0 && t.parentLabel !== void 0)) return;
		let n = r("div", "cmm-kanban__card-context");
		t.epicName !== void 0 && n.appendChild(r("span", "cmm-kanban__context-chip cmm-kanban__context-chip--epic", t.epicName)), t.sprintName !== void 0 && n.appendChild(r("span", "cmm-kanban__context-chip cmm-kanban__context-chip--sprint", t.sprintName)), t.parentId !== void 0 && t.parentLabel !== void 0 && n.appendChild(r("span", "cmm-kanban__context-chip cmm-kanban__context-chip--parent", t.parentLabel)), e.appendChild(n);
	}
	renderDescription(e, t) {
		if (t.description === void 0) return;
		let n = this.getDescriptionMaxLength(), i = t.description.length > n ? t.description.slice(0, n) + "..." : t.description;
		e.appendChild(r("div", "cmm-kanban__card-description", i));
	}
	renderProgressBar(e, t) {
		if (!(t.subtaskCount !== void 0 && t.subtaskCount > 0 || t.completionPercentage !== void 0)) return;
		let n = 0;
		t.completionPercentage === void 0 ? t.subtaskCount !== void 0 && t.subtaskCount > 0 && (n = (t.subtaskCompleted ?? 0) / t.subtaskCount * 100) : n = t.completionPercentage;
		let i = r("div", "cmm-kanban__card-progress"), a = r("div", "cmm-kanban__card-progress-track"), o = r("div", "cmm-kanban__card-progress-fill");
		o.style.width = `${String(Math.round(n))}%`, a.appendChild(o), i.appendChild(a), e.appendChild(i);
	}
	renderDueDate(e, t) {
		if (t.dueDate === void 0) return;
		let n = r("span", "cmm-kanban__card-due"), i;
		if (t.dueDate instanceof Date) i = new Date(t.dueDate), i.setHours(0, 0, 0, 0);
		else {
			let e = t.dueDate.split("-");
			i = new Date(Number(e[0]), Number(e[1]) - 1, Number(e[2])), i.setHours(0, 0, 0, 0);
		}
		let a = /* @__PURE__ */ new Date();
		a.setHours(0, 0, 0, 0);
		let o = (i.getTime() - a.getTime()) / (1e3 * 60 * 60 * 24);
		o < 0 ? n.classList.add("cmm-kanban__card-due--overdue") : o === 0 ? n.classList.add("cmm-kanban__card-due--today") : o <= 3 && n.classList.add("cmm-kanban__card-due--soon"), n.textContent = i.toLocaleDateString(), e.appendChild(n);
	}
	renderStats(e, t) {
		let n = t.commentCount !== void 0 && t.commentCount > 0, i = t.attachmentCount !== void 0 && t.attachmentCount > 0;
		if (!n && !i) return;
		let a = r("div", "cmm-kanban__card-stats");
		n && a.appendChild(r("span", "cmm-kanban__card-stat", String(t.commentCount))), i && a.appendChild(r("span", "cmm-kanban__card-stat", String(t.attachmentCount))), e.appendChild(a);
	}
	renderAssignees(e, t) {
		if (t.assignees !== void 0 && t.assignees.length > 0) {
			let n = r("div", "cmm-kanban__card-assignees"), i = this.getMaxAssigneeAvatars(), a = t.assignees.slice(0, i), o = t.assignees.length - i;
			for (let e of a) {
				let t = r("span", "cmm-kanban__card-avatar");
				t.title = e.name, e.avatarUrl === void 0 ? t.textContent = e.name.charAt(0).toUpperCase() : t.style.backgroundImage = `url(${e.avatarUrl})`, n.appendChild(t);
			}
			o > 0 && n.appendChild(r("span", "cmm-kanban__card-assignees-overflow", `+${String(o)}`)), e.appendChild(n);
		} else if (t.assignee !== void 0) {
			let n = r("div", "cmm-kanban__card-footer");
			n.appendChild(r("span", "cmm-kanban__card-assignee", t.assignee)), e.appendChild(n);
		}
	}
	renderLabels(e, t) {
		if (t.labels === void 0 || t.labels.length === 0) return;
		let n = r("div", "cmm-kanban__card-labels"), i = this.getMaxLabels(), a = t.labels.slice(0, i), o = t.labels.length - i;
		for (let e of a) {
			let t = r("span", "cmm-kanban__card-label", e.text);
			t.style.backgroundColor = e.color, n.appendChild(t);
		}
		o > 0 && n.appendChild(r("span", "cmm-kanban__card-labels-overflow", `+${String(o)}`)), e.appendChild(n);
	}
	renderActions(e, t, n) {
		if (this.config.actions === void 0 || this.config.actions.length === 0) return;
		let i = this.config.actions.filter((e) => e.visible === void 0 ? !0 : e.visible(t, n));
		if (i.length === 0) return;
		let a = r("div", "cmm-kanban__card-actions");
		for (let e of i) {
			let n = r("button", "cmm-kanban__action-btn", (e.icon ?? "") + e.label);
			n.dataset.actionId = e.id, n.addEventListener("click", (n) => {
				n.stopPropagation(), this.config.onCardAction?.(e.id, t);
			}), a.appendChild(n);
		}
		e.appendChild(a);
	}
	isDraggable() {
		return this.config.draggable !== !1;
	}
	setupDragSource(e, t, n) {
		e.addEventListener("dragstart", (r) => {
			this.dragState = {
				cardId: t,
				fromColumnId: n
			}, e.classList.add("cmm-kanban__card--dragging"), r.dataTransfer !== null && (r.dataTransfer.effectAllowed = "move", r.dataTransfer.setData("text/plain", t));
		}), e.addEventListener("dragend", () => {
			if (e.classList.remove("cmm-kanban__card--dragging"), this.dragState = null, this.wrapperEl !== null) {
				let e = this.wrapperEl.querySelectorAll(".cmm-kanban__card-list--drag-over");
				for (let t of e) t.classList.remove("cmm-kanban__card-list--drag-over");
			}
		});
	}
	setupDropZone(e, t) {
		e.addEventListener("dragover", (t) => {
			t.preventDefault(), t.dataTransfer !== null && (t.dataTransfer.dropEffect = "move"), e.classList.add("cmm-kanban__card-list--drag-over");
		}), e.addEventListener("dragleave", (t) => {
			let n = t.relatedTarget;
			n instanceof Node && e.contains(n) || e.classList.remove("cmm-kanban__card-list--drag-over");
		}), e.addEventListener("drop", (n) => {
			if (n.preventDefault(), e.classList.remove("cmm-kanban__card-list--drag-over"), this.dragState === null) return;
			let { cardId: r, fromColumnId: i } = this.dragState, a = this.calculateDropIndex(e, n.clientY);
			this.dragState = null, this.performMove(r, i, t, a);
		});
	}
	calculateDropIndex(e, t) {
		let n = e.querySelectorAll(".cmm-kanban__card"), r = n.length;
		for (let e = 0; e < n.length; e++) {
			let i = n[e];
			if (i === void 0) continue;
			let a = i.getBoundingClientRect();
			if (t < a.top + a.height / 2) {
				r = e;
				break;
			}
		}
		return r;
	}
	extractCard(e) {
		for (let t of this.columns) {
			let n = t.cards.findIndex((t) => t.id === e);
			if (n >= 0) {
				let e = t.cards.splice(n, 1)[0];
				if (e !== void 0) return {
					card: e,
					fromColumnId: t.id
				};
			}
		}
	}
	performMove(e, t, n, r) {
		let i = this.extractCard(e);
		if (i === void 0) return;
		let { card: a, fromColumnId: o } = i, s = this.columns.find((e) => e.id === n);
		if (s === void 0) return;
		let c = Math.min(r, s.cards.length);
		s.cards.splice(c, 0, a);
		let l = this.config.onCardMove?.(e, o, n, c);
		if (this.render(), l instanceof Promise) {
			let t = { ...a };
			l.then(() => {
				let t = this.container.querySelector(`[data-card-id="${e}"]`);
				t !== null && t.classList.add("cmm-kanban__card--landed");
			}).catch(() => {
				let r = this.columns.find((e) => e.id === n);
				if (r !== void 0) {
					let t = r.cards.findIndex((t) => t.id === e);
					t >= 0 && r.cards.splice(t, 1);
				}
				let i = this.columns.find((e) => e.id === o);
				i !== void 0 && i.cards.push(t), this.render();
			});
		}
	}
};
//#endregion
export { a as Kanban };

//# sourceMappingURL=index.js.map