//#region src/components/copy-button/index.ts
function e(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`CopyButton: container not found for selector "${e}"`);
		return t;
	}
	return e;
}
var t = class {
	config;
	containerEl;
	buttonEl;
	defaultLabel;
	copiedLabel;
	copiedDuration;
	resetTimer = null;
	destroyed = !1;
	constructor(t) {
		this.config = t, this.containerEl = e(t.container), this.defaultLabel = t.label ?? "Copy", this.copiedLabel = t.copiedLabel ?? "Copied!", this.copiedDuration = t.copiedDuration ?? 2e3, this.buttonEl = document.createElement("button"), this.buttonEl.className = "cmm-copy-button", this.buttonEl.textContent = this.defaultLabel, this.buttonEl.addEventListener("click", this.handleClick), this.containerEl.appendChild(this.buttonEl);
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.resetTimer !== null && clearTimeout(this.resetTimer), this.buttonEl.removeEventListener("click", this.handleClick), this.buttonEl.parentNode && this.buttonEl.parentNode.removeChild(this.buttonEl));
	}
	handleClick = () => {
		let e = this.config.getText();
		this.copyToClipboard(e);
	};
	async copyToClipboard(e) {
		try {
			await navigator.clipboard.writeText(e), this.showCopiedFeedback();
		} catch {}
	}
	showCopiedFeedback() {
		this.resetTimer !== null && clearTimeout(this.resetTimer), this.buttonEl.textContent = this.copiedLabel, this.buttonEl.classList.add("cmm-copy-button--copied"), this.resetTimer = setTimeout(() => {
			this.destroyed || (this.buttonEl.textContent = this.defaultLabel, this.buttonEl.classList.remove("cmm-copy-button--copied")), this.resetTimer = null;
		}, this.copiedDuration);
	}
};
//#endregion
export { t as CopyButton };

//# sourceMappingURL=index.js.map