//#region src/components/log-toolbar/index.ts
function e(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`LogToolbar: container not found for selector "${e}"`);
		return t;
	}
	return e;
}
var t = class {
	containerEl;
	rootEl;
	searchEl;
	followBtn;
	wrapBtn;
	copyBtn;
	downloadBtn;
	follow;
	wrap;
	debounceTimer = null;
	destroyed = !1;
	config;
	constructor(t) {
		this.config = t, this.containerEl = e(t.container), this.follow = t.follow ?? !0, this.wrap = t.wrap ?? !0, this.rootEl = document.createElement("div"), this.rootEl.className = "cmm-log-toolbar", this.searchEl = document.createElement("input"), this.searchEl.type = "text", this.searchEl.className = "cmm-log-toolbar__search", this.searchEl.placeholder = "Search logs...", this.searchEl.addEventListener("input", this.handleSearch), this.rootEl.appendChild(this.searchEl), this.followBtn = this.createToggle("Follow", this.follow, this.handleFollow), this.rootEl.appendChild(this.followBtn), this.wrapBtn = this.createToggle("Wrap", this.wrap, this.handleWrap), this.rootEl.appendChild(this.wrapBtn), this.copyBtn = document.createElement("button"), this.copyBtn.className = "cmm-log-toolbar__btn", this.copyBtn.textContent = "Copy", this.copyBtn.addEventListener("click", this.handleCopy), this.rootEl.appendChild(this.copyBtn), this.downloadBtn = document.createElement("button"), this.downloadBtn.className = "cmm-log-toolbar__btn", this.downloadBtn.textContent = "Download", this.downloadBtn.addEventListener("click", this.handleDownload), this.rootEl.appendChild(this.downloadBtn), this.containerEl.appendChild(this.rootEl);
	}
	getSearchQuery() {
		return this.searchEl.value;
	}
	getFollowEnabled() {
		return this.follow;
	}
	setFollow(e) {
		this.follow = e, this.followBtn.classList.toggle("cmm-log-toolbar__btn--active", e), this.followBtn.setAttribute("aria-pressed", String(e));
	}
	setWrap(e) {
		this.wrap = e, this.wrapBtn.classList.toggle("cmm-log-toolbar__btn--active", e);
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.debounceTimer !== null && clearTimeout(this.debounceTimer), this.searchEl.removeEventListener("input", this.handleSearch), this.followBtn.removeEventListener("click", this.handleFollow), this.wrapBtn.removeEventListener("click", this.handleWrap), this.copyBtn.removeEventListener("click", this.handleCopy), this.downloadBtn.removeEventListener("click", this.handleDownload), this.rootEl.parentNode && this.rootEl.parentNode.removeChild(this.rootEl));
	}
	createToggle(e, t, n) {
		let r = document.createElement("button");
		return r.className = "cmm-log-toolbar__btn", t && r.classList.add("cmm-log-toolbar__btn--active"), r.textContent = e, r.setAttribute("aria-pressed", String(t)), r.addEventListener("click", n), r;
	}
	handleSearch = () => {
		this.debounceTimer !== null && clearTimeout(this.debounceTimer), this.debounceTimer = setTimeout(() => {
			this.config.onSearch?.(this.searchEl.value);
		}, this.config.debounceMs ?? 150);
	};
	handleFollow = () => {
		this.follow = !this.follow, this.followBtn.classList.toggle("cmm-log-toolbar__btn--active", this.follow), this.followBtn.setAttribute("aria-pressed", String(this.follow)), this.config.onFollowChange?.(this.follow);
	};
	handleWrap = () => {
		this.wrap = !this.wrap, this.wrapBtn.classList.toggle("cmm-log-toolbar__btn--active", this.wrap), this.config.onWrapChange?.(this.wrap);
	};
	handleCopy = () => {
		let e = this.config.getText?.() ?? "";
		navigator.clipboard.writeText(e).then(() => {
			this.copyBtn.textContent = "Copied!", this.copyBtn.classList.add("cmm-log-toolbar__btn--copied"), setTimeout(() => {
				this.destroyed || (this.copyBtn.textContent = "Copy", this.copyBtn.classList.remove("cmm-log-toolbar__btn--copied"));
			}, 2e3);
		}).catch(() => {});
	};
	handleDownload = () => {
		let e = this.config.getText?.() ?? "", t = new Blob([e], { type: "text/plain" }), n = URL.createObjectURL(t), r = document.createElement("a");
		r.href = n, r.download = `${this.config.downloadName ?? "logs"}.log`, r.style.display = "none", document.body.appendChild(r), r.click(), r.remove(), setTimeout(() => {
			URL.revokeObjectURL(n);
		}, 0);
	};
};
//#endregion
export { t as LogToolbar };

//# sourceMappingURL=index.js.map