//#region src/components/pagination/index.ts
var e = "http://www.w3.org/2000/svg";
function t(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`Pagination: container not found for selector "${e}"`);
		return t;
	}
	return e;
}
function n(t, n) {
	let r = document.createElementNS(e, t);
	for (let [e, t] of Object.entries(n)) r.setAttribute(e, t);
	return r;
}
function r() {
	let e = n("svg", {
		viewBox: "0 0 24 24",
		fill: "none",
		stroke: "currentColor",
		"stroke-width": "2",
		"stroke-linecap": "round",
		"stroke-linejoin": "round",
		"aria-hidden": "true"
	});
	return e.classList.add("cmm-pagination__icon"), e;
}
function i() {
	let e = r();
	return e.appendChild(n("path", { d: "m15 18-6-6 6-6" })), e;
}
function a() {
	let e = r();
	return e.appendChild(n("path", { d: "m9 18 6-6-6-6" })), e;
}
function o() {
	let e = r();
	return e.appendChild(n("circle", {
		cx: "12",
		cy: "12",
		r: "1"
	})), e.appendChild(n("circle", {
		cx: "19",
		cy: "12",
		r: "1"
	})), e.appendChild(n("circle", {
		cx: "5",
		cy: "12",
		r: "1"
	})), e;
}
function s(e, t, n) {
	if (t <= n) return Array.from({ length: t }, (e, t) => t + 1);
	let r = Math.max(1, n - 4), i = Math.max(2, e - Math.floor((r - 1) / 2)), a = Math.min(t - 1, i + r - 1), o = Math.max(2, a - r + 1);
	o === 2 && a < t - 1 && (a = Math.min(t - 1, a + 1)), a === t - 1 && o > 2 && (o = Math.max(2, o - 1));
	let s = [1];
	o > 2 && s.push("ellipsis");
	for (let e = o; e <= a; e++) s.push(e);
	return a < t - 1 && s.push("ellipsis"), s.push(t), s;
}
var c = class {
	config;
	containerEl;
	rootEl;
	prevBtn;
	nextBtn;
	infoEl;
	pagesEl;
	totalPages;
	maxButtons;
	currentPage;
	hasMore;
	destroyed = !1;
	pageListeners = [];
	constructor(e) {
		if (this.config = e, this.containerEl = t(e.container), this.currentPage = e.page ?? 1, this.hasMore = e.hasMore === !0, this.totalPages = typeof e.totalPages == "number" && e.totalPages > 0 ? e.totalPages : null, this.maxButtons = Math.max(5, e.siblingCount ?? 7), this.rootEl = document.createElement("div"), this.rootEl.className = "cmm-pagination", this.rootEl.dataset.slot = "pagination", this.rootEl.setAttribute("role", "navigation"), this.rootEl.setAttribute("aria-label", "Pagination"), e.className !== void 0 && e.className.trim() !== "") for (let t of e.className.split(/\s+/)) this.rootEl.classList.add(t);
		this.prevBtn = document.createElement("button"), this.prevBtn.type = "button", this.prevBtn.className = "cmm-pagination__button cmm-pagination__button--prev", this.prevBtn.dataset.slot = "pagination-previous", this.prevBtn.setAttribute("aria-label", "Go to previous page"), this.prevBtn.appendChild(i()), this.prevBtn.appendChild(this.makeLabel("Previous")), this.prevBtn.addEventListener("click", this.handlePrev), this.pagesEl = document.createElement("div"), this.pagesEl.className = "cmm-pagination__pages", this.infoEl = document.createElement("span"), this.infoEl.className = "cmm-pagination__info", this.nextBtn = document.createElement("button"), this.nextBtn.type = "button", this.nextBtn.className = "cmm-pagination__button cmm-pagination__button--next", this.nextBtn.dataset.slot = "pagination-next", this.nextBtn.setAttribute("aria-label", "Go to next page"), this.nextBtn.appendChild(this.makeLabel("Next")), this.nextBtn.appendChild(a()), this.nextBtn.addEventListener("click", this.handleNext), this.rootEl.appendChild(this.prevBtn), this.totalPages === null ? this.rootEl.appendChild(this.infoEl) : this.rootEl.appendChild(this.pagesEl), this.rootEl.appendChild(this.nextBtn), this.containerEl.appendChild(this.rootEl), this.render();
	}
	makeLabel(e) {
		let t = document.createElement("span");
		return t.className = "cmm-pagination__label", t.textContent = e, t;
	}
	setPage(e, t) {
		this.currentPage = e, t !== void 0 && (this.hasMore = t), this.render();
	}
	getPage() {
		return this.currentPage;
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.prevBtn.removeEventListener("click", this.handlePrev), this.nextBtn.removeEventListener("click", this.handleNext), this.clearPageListeners(), this.rootEl.parentNode && this.rootEl.parentNode.removeChild(this.rootEl));
	}
	get canGoNext() {
		return this.totalPages === null ? this.hasMore : this.currentPage < this.totalPages;
	}
	render() {
		this.infoEl.textContent = `Page ${String(this.currentPage)}`, this.totalPages !== null && this.renderPages(), this.currentPage <= 1 ? (this.prevBtn.disabled = !0, this.prevBtn.classList.add("cmm-pagination__button--disabled")) : (this.prevBtn.disabled = !1, this.prevBtn.classList.remove("cmm-pagination__button--disabled")), this.canGoNext ? (this.nextBtn.disabled = !1, this.nextBtn.classList.remove("cmm-pagination__button--disabled")) : (this.nextBtn.disabled = !0, this.nextBtn.classList.add("cmm-pagination__button--disabled"));
	}
	renderPages() {
		if (this.totalPages === null) return;
		this.clearPageListeners(), this.pagesEl.replaceChildren();
		let e = s(this.currentPage, this.totalPages, this.maxButtons);
		for (let t of e) t === "ellipsis" ? this.pagesEl.appendChild(this.makeEllipsis()) : this.pagesEl.appendChild(this.makePageLink(t));
	}
	makePageLink(e) {
		let t = document.createElement("button");
		t.type = "button", t.className = "cmm-pagination__button cmm-pagination__button--page", t.dataset.slot = "pagination-link", t.dataset.page = String(e), t.textContent = String(e), e === this.currentPage && (t.classList.add("cmm-pagination__button--active"), t.setAttribute("aria-current", "page"));
		let n = () => {
			this.goToPage(e);
		};
		return t.addEventListener("click", n), this.pageListeners.push({
			el: t,
			fn: n
		}), t;
	}
	makeEllipsis() {
		let e = document.createElement("span");
		e.className = "cmm-pagination__ellipsis", e.dataset.slot = "pagination-ellipsis", e.setAttribute("aria-hidden", "true"), e.appendChild(o());
		let t = document.createElement("span");
		return t.className = "cmm-pagination__sr-only", t.textContent = "More pages", e.appendChild(t), e;
	}
	clearPageListeners() {
		for (let { el: e, fn: t } of this.pageListeners) e.removeEventListener("click", t);
		this.pageListeners = [];
	}
	goToPage(e) {
		let t = this.totalPages, n = t === null ? Math.max(1, e) : Math.min(Math.max(1, e), t);
		n !== this.currentPage && (this.currentPage = n, this.render(), this.config.onPageChange?.(this.currentPage));
	}
	handlePrev = () => {
		this.currentPage > 1 && (--this.currentPage, this.render(), this.config.onPageChange && this.config.onPageChange(this.currentPage));
	};
	handleNext = () => {
		this.canGoNext && (this.currentPage += 1, this.render(), this.config.onPageChange && this.config.onPageChange(this.currentPage));
	};
};
//#endregion
export { c as Pagination };

//# sourceMappingURL=index.js.map