import { A as e, K as t, Ot as n, W as r, X as i, bt as a, f as o, ot as s, rt as c } from "../dist-DgSmYkuV.js";
import { i as l, n as u, r as d } from "../base-B7MLNUXu.js";
//#region src/editors/diff-viewer.ts
function f(e, t) {
	let n = e.split("\n"), r = t.split("\n"), i = [], a = n.length, o = r.length, s = Array.from({ length: a + 1 }, () => Array(o + 1).fill(0));
	for (let e = 1; e <= a; e++) for (let t = 1; t <= o; t++) {
		let i = s[e - 1], a = s[e];
		!i || !a || (n[e - 1] === r[t - 1] ? a[t] = (i[t - 1] ?? 0) + 1 : a[t] = Math.max(i[t] ?? 0, a[t - 1] ?? 0));
	}
	let c = a, l = o, u = [];
	for (; c > 0 || l > 0;) {
		let e = s[c], t = s[c - 1];
		c > 0 && l > 0 && n[c - 1] === r[l - 1] ? (u.push({
			type: "equal",
			originalLine: n[c - 1] ?? "",
			modifiedLine: r[l - 1] ?? ""
		}), c--, l--) : l > 0 && (c === 0 || (e?.[l - 1] ?? 0) >= (t?.[l] ?? 0)) ? (u.push({
			type: "added",
			originalLine: "",
			modifiedLine: r[l - 1] ?? ""
		}), l--) : (u.push({
			type: "removed",
			originalLine: n[c - 1] ?? "",
			modifiedLine: ""
		}), c--);
	}
	for (let e = u.length - 1; e >= 0; e--) {
		let t = u[e];
		t && i.push(t);
	}
	return i;
}
var p = t.theme({
	".cmm-diff-added": { backgroundColor: "var(--cmm-diff-added-bg, rgba(34, 197, 94, 0.18))" },
	".cmm-diff-added .cm-gutterElement": {
		backgroundColor: "var(--cmm-diff-added-gutter, rgba(34, 197, 94, 0.28))",
		color: "var(--cmm-diff-added-gutter-text, #34d399)"
	},
	".cmm-diff-removed": { backgroundColor: "var(--cmm-diff-removed-bg, rgba(239, 68, 68, 0.18))" },
	".cmm-diff-removed .cm-gutterElement": {
		backgroundColor: "var(--cmm-diff-removed-gutter, rgba(239, 68, 68, 0.28))",
		color: "var(--cmm-diff-removed-gutter-text, #fb7185)"
	},
	".cmm-diff-gap": { backgroundImage: "var(--cmm-diff-gap-stripe, repeating-linear-gradient(135deg, rgba(148,163,184,0.05) 0 6px, transparent 6px 12px))" }
}), m = r.line({ attributes: { class: "cmm-diff-added" } }), h = r.line({ attributes: { class: "cmm-diff-removed" } }), g = r.line({ attributes: { class: "cmm-diff-gap" } });
function _(e) {
	return n.define({
		create(t) {
			let n = [], i = t.doc.lines;
			for (let r = 0; r < i && r < e.length; r++) {
				let i = e[r];
				if (i == null) continue;
				let a = t.doc.line(r + 1);
				i === "added" ? n.push(m.range(a.from)) : i === "removed" ? n.push(h.range(a.from)) : n.push(g.range(a.from));
			}
			return r.set(n, !0);
		},
		update(e) {
			return e;
		},
		provide: (e) => t.decorations.from(e)
	});
}
var v = class {
	container;
	originalView = null;
	modifiedView = null;
	wrapperEl = null;
	constructor(e) {
		this.container = d(e.container), this.render(e);
	}
	render(n) {
		let r = f(n.original, n.modified), d = n.lineNumbers !== !1, m = [], h = [], g = [], v = [];
		for (let e of r) e.type === "equal" ? (m.push(e.originalLine), h.push(e.modifiedLine), g.push(null), v.push(null)) : e.type === "removed" ? (m.push(e.originalLine), h.push(""), g.push("removed"), v.push("gap")) : (m.push(""), h.push(e.modifiedLine), g.push("gap"), v.push("added"));
		this.wrapperEl = document.createElement("div"), this.wrapperEl.className = "cmm-diff-viewer", this.wrapperEl.style.display = "grid", this.wrapperEl.style.gridTemplateColumns = "1fr 1fr", this.wrapperEl.style.gap = "1px", this.wrapperEl.style.backgroundColor = "var(--cmm-border, #e2e8f0)";
		let y = document.createElement("div");
		y.className = "cmm-diff-pane cmm-diff-original";
		let b = document.createElement("div");
		b.className = "cmm-diff-pane cmm-diff-modified", this.wrapperEl.appendChild(y), this.wrapperEl.appendChild(b), this.container.appendChild(this.wrapperEl);
		let x = [
			c(),
			i(),
			a.readOnly.of(!0),
			e(l),
			e(o, { fallback: !0 }),
			u,
			p,
			...d ? [s()] : []
		];
		this.originalView = new t({
			state: a.create({
				doc: m.join("\n"),
				extensions: [...x, _(g)]
			}),
			parent: y
		}), this.modifiedView = new t({
			state: a.create({
				doc: h.join("\n"),
				extensions: [...x, _(v)]
			}),
			parent: b
		});
		let S = (e, t) => {
			let n = e.scrollDOM;
			n.addEventListener("scroll", () => {
				t.scrollDOM.scrollTop = n.scrollTop, t.scrollDOM.scrollLeft = n.scrollLeft;
			});
		};
		S(this.originalView, this.modifiedView), S(this.modifiedView, this.originalView);
	}
	setContent(e, t) {
		this.destroy(), this.render({
			container: this.container,
			original: e,
			modified: t
		});
	}
	getOriginal() {
		return this.originalView?.state.doc.toString() ?? "";
	}
	getModified() {
		return this.modifiedView?.state.doc.toString() ?? "";
	}
	destroy() {
		this.originalView?.destroy(), this.modifiedView?.destroy(), this.originalView = null, this.modifiedView = null, this.wrapperEl &&= (this.wrapperEl.remove(), null);
	}
};
//#endregion
export { v as DiffViewer };

//# sourceMappingURL=diff-viewer.js.map