import { t as e } from "../../a11y-ZYZ3T617.js";
//#region src/components/copy-button/index.ts
var t = "http://www.w3.org/2000/svg";
function n(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;
}
function r(e, n) {
	let r = document.createElementNS(t, e);
	for (let [e, t] of Object.entries(n)) r.setAttribute(e, t);
	return r;
}
function i() {
	let e = r("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-copy-button__icon"), e;
}
function a() {
	let e = i();
	return e.appendChild(r("rect", {
		width: "14",
		height: "14",
		x: "8",
		y: "8",
		rx: "2",
		ry: "2"
	})), e.appendChild(r("path", { d: "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" })), e;
}
function o() {
	let e = i();
	return e.appendChild(r("path", { d: "M20 6 9 17l-5-5" })), e;
}
var s = class {
	config;
	containerEl;
	buttonEl;
	defaultLabel;
	copiedLabel;
	copiedDuration;
	iconOnly;
	resetTimer = null;
	destroyed = !1;
	constructor(e) {
		this.config = e, this.containerEl = n(e.container), this.defaultLabel = e.label ?? "Copy", this.copiedLabel = e.copiedLabel ?? "Copied!", this.copiedDuration = e.copiedDuration ?? 2e3, this.iconOnly = e.iconOnly ?? !1;
		let t = e.variant ?? "default", r = this.iconOnly ? "icon" : e.size ?? "default";
		if (this.buttonEl = document.createElement("button"), this.buttonEl.type = "button", this.buttonEl.dataset.slot = "copy-button", this.buttonEl.className = "cmm-copy-button", this.buttonEl.classList.add(`cmm-copy-button--${t}`), this.buttonEl.classList.add(`cmm-copy-button--size-${r}`), e.className !== void 0 && e.className.trim() !== "") for (let t of e.className.split(/\s+/)) this.buttonEl.classList.add(t);
		e.disabled === !0 && (this.buttonEl.disabled = !0), this.renderContent(!1), 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));
	}
	renderContent(e) {
		let t = e ? o() : a();
		this.buttonEl.replaceChildren(t);
		let n = e ? this.copiedLabel : this.defaultLabel;
		if (this.iconOnly) this.buttonEl.setAttribute("aria-label", n);
		else {
			let e = document.createElement("span");
			e.className = "cmm-copy-button__label", e.textContent = n, this.buttonEl.appendChild(e);
		}
		e ? this.buttonEl.dataset.copied = "true" : delete this.buttonEl.dataset.copied;
	}
	handleClick = () => {
		let e = this.config.getText();
		this.copyToClipboard(e);
	};
	async copyToClipboard(e) {
		try {
			await navigator.clipboard.writeText(e), this.showCopiedFeedback(), this.config.onCopy?.(e);
		} catch {}
	}
	showCopiedFeedback() {
		this.resetTimer !== null && clearTimeout(this.resetTimer), this.renderContent(!0), this.buttonEl.classList.add("cmm-copy-button--copied"), e(this.copiedLabel, "polite"), this.resetTimer = setTimeout(() => {
			this.destroyed || (this.renderContent(!1), this.buttonEl.classList.remove("cmm-copy-button--copied")), this.resetTimer = null;
		}, this.copiedDuration);
	}
};
//#endregion
export { s as CopyButton };

//# sourceMappingURL=index.js.map