import { n as e, t } from "../../utils-C_mWfcFX.js";
import { A as n, K as r, bt as i, f as a, ot as o } from "../../dist-DgSmYkuV.js";
import { i as s, n as c } from "../../base-B7MLNUXu.js";
import { fluxl as l } from "../../editors/languages/fluxl.js";
import { yaral as u } from "../../editors/languages/yaral.js";
import { yara as d } from "../../editors/languages/yara.js";
import { t as f } from "../../json-D8YcUjFo.js";
import { t as p } from "../../sql-BTWxpluA.js";
import { t as m } from "../../yaml-BAVAG9c4.js";
//#region src/display/code-block/index.ts
var h = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><rect x=\"9\" y=\"9\" width=\"13\" height=\"13\" rx=\"2\" ry=\"2\"/><path d=\"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1\"/></svg>";
function g(e) {
	switch (e) {
		case "json": return f();
		case "yaml":
		case "yml": return m();
		case "sql": return p();
		case "fluxl": return l();
		case "yaral": return u();
		case "yara": return d();
		default: return [];
	}
}
var _ = r.theme({
	"&": { fontSize: "13px" },
	"&.cm-focused": { outline: "none" },
	".cm-content": {
		padding: "12px 0",
		cursor: "text"
	},
	".cm-gutters": { cursor: "default" },
	".cm-activeLine": { backgroundColor: "transparent" },
	".cm-activeLineGutter": { backgroundColor: "transparent" }
}), v = class {
	el;
	codeBlockId;
	showLineNumbers;
	showCopyButton;
	maxHeight;
	language;
	currentCode;
	editorView = null;
	editorContainer = null;
	copyBtnEl = null;
	copyTimeoutId = null;
	constructor(n) {
		this.codeBlockId = e("cmm-code-block"), this.currentCode = n.code, this.language = n.language, this.showLineNumbers = n.showLineNumbers === !0, this.showCopyButton = n.showCopyButton !== !1, this.maxHeight = n.maxHeight, this.el = document.createElement("div"), this.el.id = this.codeBlockId, this.el.className = "cmm-code-block", n.className && this.el.classList.add(n.className), this.buildContent(), n.container !== void 0 && t(n.container).appendChild(this.el);
	}
	setCode(e) {
		this.currentCode = e, this.editorView && this.editorView.dispatch({ changes: {
			from: 0,
			to: this.editorView.state.doc.length,
			insert: e
		} });
	}
	destroy() {
		this.copyTimeoutId !== null && (clearTimeout(this.copyTimeoutId), this.copyTimeoutId = null), this.editorView &&= (this.editorView.destroy(), null), this.el.remove();
	}
	buildContent() {
		if (this.language !== void 0 || this.showCopyButton) {
			let e = document.createElement("div");
			e.className = "cmm-code-block__header";
			let t = document.createElement("span");
			if (t.className = "cmm-code-block__language", t.textContent = this.language ?? "", e.appendChild(t), this.showCopyButton) {
				let t = document.createElement("button");
				t.className = "cmm-code-block__copy", t.setAttribute("aria-label", "Copy code"), t.type = "button", t.innerHTML = `${h} Copy`, t.addEventListener("click", () => {
					this.handleCopy();
				}), e.appendChild(t), this.copyBtnEl = t;
			}
			this.el.appendChild(e);
		}
		this.editorContainer = document.createElement("div"), this.editorContainer.className = "cmm-code-block__editor", this.maxHeight !== void 0 && (this.editorContainer.style.maxHeight = this.maxHeight, this.editorContainer.style.overflow = "auto");
		let e = [
			i.readOnly.of(!0),
			r.editable.of(!1),
			n(s),
			n(a, { fallback: !0 }),
			c,
			_,
			g(this.language)
		];
		this.showLineNumbers && e.push(o()), this.editorView = new r({
			state: i.create({
				doc: this.currentCode,
				extensions: e
			}),
			parent: this.editorContainer
		}), this.el.appendChild(this.editorContainer);
	}
	handleCopy() {
		navigator.clipboard.writeText(this.currentCode).then(() => {
			this.copyBtnEl && (this.copyBtnEl.innerHTML = `${h} Copied!`, this.copyBtnEl.classList.add("cmm-code-block__copy--copied"), this.copyTimeoutId !== null && clearTimeout(this.copyTimeoutId), this.copyTimeoutId = setTimeout(() => {
				this.copyBtnEl && (this.copyBtnEl.innerHTML = `${h} Copy`, this.copyBtnEl.classList.remove("cmm-code-block__copy--copied")), this.copyTimeoutId = null;
			}, 2e3));
		});
	}
};
//#endregion
export { v as CodeBlock };

//# sourceMappingURL=index.js.map