import { n as e, t } from "../../utils-CSEWuKHo.js";
import { A as n, K as r, bt as i, f as a, ot as o } from "../../dist-BltQ1OD_.js";
import { i as s, n as c } from "../../base-iRd9JDTW.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-BUQMjFP3.js";
import { t as p } from "../../sql-Bx84ZRYw.js";
import { t as m } from "../../yaml-CNUaja3z.js";
//#region src/display/code-block/index.ts
function h() {
	let e = document.createElementNS("http://www.w3.org/2000/svg", "svg");
	e.setAttribute("width", "14"), e.setAttribute("height", "14"), e.setAttribute("viewBox", "0 0 24 24"), e.setAttribute("fill", "none"), e.setAttribute("stroke", "currentColor"), e.setAttribute("stroke-width", "2"), e.setAttribute("stroke-linecap", "round"), e.setAttribute("stroke-linejoin", "round");
	let t = document.createElementNS("http://www.w3.org/2000/svg", "rect");
	t.setAttribute("x", "9"), t.setAttribute("y", "9"), t.setAttribute("width", "13"), t.setAttribute("height", "13"), t.setAttribute("rx", "2"), t.setAttribute("ry", "2"), e.appendChild(t);
	let n = document.createElementNS("http://www.w3.org/2000/svg", "path");
	return n.setAttribute("d", "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"), e.appendChild(n), e;
}
function g(e) {
	switch (e) {
		case "json": return f();
		case "yaml":
		case "yml":
		case "sigma": 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", this.setCopyButtonLabel(t, "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());
		let t = i.create({
			doc: this.currentCode,
			extensions: e
		});
		this.editorView = new r({
			state: t,
			parent: this.editorContainer
		}), this.el.appendChild(this.editorContainer);
	}
	handleCopy() {
		navigator.clipboard.writeText(this.currentCode).then(() => {
			this.copyBtnEl && (this.setCopyButtonLabel(this.copyBtnEl, "Copied!"), this.copyBtnEl.classList.add("cmm-code-block__copy--copied"), this.copyTimeoutId !== null && clearTimeout(this.copyTimeoutId), this.copyTimeoutId = setTimeout(() => {
				this.copyBtnEl && (this.setCopyButtonLabel(this.copyBtnEl, "Copy"), this.copyBtnEl.classList.remove("cmm-code-block__copy--copied")), this.copyTimeoutId = null;
			}, 2e3));
		});
	}
	setCopyButtonLabel(e, t) {
		e.replaceChildren(h(), document.createTextNode(` ${t}`));
	}
};
//#endregion
export { v as CodeBlock };

//# sourceMappingURL=index.js.map