//#region src/components/split-pane/index.ts
function e(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`SplitPane container not found: ${e}`);
		return t;
	}
	return e;
}
function t(e, t) {
	if (e === void 0) return 50;
	if (typeof e == "number") return t > 0 ? e / t * 100 : 50;
	if (e.endsWith("%")) {
		let t = parseFloat(e);
		return Number.isNaN(t) ? 50 : t;
	}
	if (e.endsWith("px")) {
		let n = parseFloat(e);
		return !Number.isNaN(n) && t > 0 ? n / t * 100 : 50;
	}
	let n = parseFloat(e);
	return Number.isNaN(n) ? 50 : n;
}
var n = class {
	container;
	config;
	wrapperEl = null;
	paneStates;
	isDragging = !1;
	handleMouseMove;
	handleMouseUp;
	constructor(n) {
		this.container = e(n.container), this.config = n;
		let r = n.orientation ?? "horizontal", i = this.container.getBoundingClientRect(), a = r === "horizontal" ? i.width : i.height, [o, s] = n.panes, c = t(o.defaultSize, a), l = 100 - c;
		this.paneStates = [{
			config: o,
			element: document.createElement("div"),
			sizePercent: c,
			collapsed: !1,
			savedSize: c
		}, {
			config: s,
			element: document.createElement("div"),
			sizePercent: l,
			collapsed: !1,
			savedSize: l
		}], this.handleMouseMove = (e) => {
			this.onMouseMove(e);
		}, this.handleMouseUp = () => {
			this.onMouseUp();
		}, this.buildDom();
	}
	getPane(e) {
		for (let t of this.paneStates) if (t.config.id === e) return t.element;
		return null;
	}
	collapse(e) {
		let t = this.findPaneState(e);
		if (!t || t.config.collapsible !== !0 || t.collapsed) return;
		t.savedSize = t.sizePercent, t.collapsed = !0, t.element.classList.add("cmm-split__pane--collapsed");
		let n = this.getOtherPane(e);
		n && (n.sizePercent += t.sizePercent), t.sizePercent = 0, this.applySizes();
	}
	expand(e) {
		let t = this.findPaneState(e);
		if (!t || !t.collapsed) return;
		t.collapsed = !1, t.element.classList.remove("cmm-split__pane--collapsed");
		let n = t.savedSize, r = this.getOtherPane(e);
		r && (r.sizePercent -= n, r.sizePercent < 0 && (r.sizePercent = 0)), t.sizePercent = n, this.applySizes();
	}
	setSizes(e) {
		let n = this.config.orientation ?? "horizontal", r = this.container.getBoundingClientRect(), i = n === "horizontal" ? r.width : r.height;
		for (let n of this.paneStates) {
			let r = e[n.config.id];
			r !== void 0 && (n.sizePercent = t(r, i), n.collapsed = !1, n.element.classList.remove("cmm-split__pane--collapsed"));
		}
		this.applySizes();
	}
	destroy() {
		document.removeEventListener("mousemove", this.handleMouseMove), document.removeEventListener("mouseup", this.handleMouseUp), this.container.innerHTML = "", this.wrapperEl = null;
	}
	buildDom() {
		this.container.innerHTML = "";
		let e = this.config.orientation ?? "horizontal", t = this.config.gutterSize ?? 4, n = document.createElement("div");
		n.className = `cmm-split cmm-split--${e}`, n.style.display = "flex", n.style.width = "100%", n.style.height = "100%", e === "vertical" ? n.style.flexDirection = "column" : n.style.flexDirection = "row", this.wrapperEl = n;
		let r = this.paneStates[0].element;
		r.className = "cmm-split__pane", r.dataset.paneId = this.paneStates[0].config.id, r.style.overflow = "auto", n.appendChild(r);
		let i = document.createElement("div");
		i.className = "cmm-split__gutter", e === "horizontal" ? (i.style.width = `${String(t)}px`, i.style.cursor = "col-resize") : (i.style.height = `${String(t)}px`, i.style.cursor = "row-resize"), i.style.flexShrink = "0", i.addEventListener("mousedown", (e) => {
			this.onMouseDown(e);
		}), n.appendChild(i);
		let a = this.paneStates[1].element;
		a.className = "cmm-split__pane", a.dataset.paneId = this.paneStates[1].config.id, a.style.overflow = "auto", n.appendChild(a), this.container.appendChild(n), this.applySizes();
	}
	applySizes() {
		let e = this.config.orientation ?? "horizontal", t = this.config.gutterSize ?? 4, [n, r] = this.paneStates, i = e === "horizontal" ? "width" : "height";
		n.element.style[i] = `calc(${String(n.sizePercent)}% - ${String(t / 2)}px)`, r.element.style[i] = `calc(${String(r.sizePercent)}% - ${String(t / 2)}px)`, n.element.style.flexShrink = "0", r.element.style.flexShrink = "0", this.config.onResize !== void 0 && (this.config.onResize(n.config.id, n.sizePercent), this.config.onResize(r.config.id, r.sizePercent));
	}
	onMouseDown(e) {
		e.preventDefault(), this.isDragging = !0, document.addEventListener("mousemove", this.handleMouseMove), document.addEventListener("mouseup", this.handleMouseUp);
	}
	onMouseMove(e) {
		if (!this.isDragging || !this.wrapperEl) return;
		let t = this.config.orientation ?? "horizontal", n = this.wrapperEl.getBoundingClientRect(), r;
		if (t === "horizontal") {
			let t = e.clientX - n.left;
			r = n.width > 0 ? t / n.width * 100 : 50;
		} else {
			let t = e.clientY - n.top;
			r = n.height > 0 ? t / n.height * 100 : 50;
		}
		let [i, a] = this.paneStates, o = i.config.minSize ?? 0, s = a.config.minSize ?? 0, c = t === "horizontal" ? n.width : n.height, l = c > 0 ? o / c * 100 : 0, u = c > 0 ? s / c * 100 : 0, d = Math.max(l, Math.min(100 - u, r)), f = 100 - d;
		f < u && (f = u, d = 100 - f), i.sizePercent = d, a.sizePercent = f, this.applySizes();
	}
	onMouseUp() {
		this.isDragging = !1, document.removeEventListener("mousemove", this.handleMouseMove), document.removeEventListener("mouseup", this.handleMouseUp);
	}
	findPaneState(e) {
		for (let t of this.paneStates) if (t.config.id === e) return t;
		return null;
	}
	getOtherPane(e) {
		return this.paneStates[0].config.id === e ? this.paneStates[1] : this.paneStates[1].config.id === e ? this.paneStates[0] : null;
	}
};
//#endregion
export { n as SplitPane };

//# sourceMappingURL=index.js.map