/** @license MIT | @cube-dev/ui-kit v0.147.0 | Cube Dev Team */
//#region src/components/fields/CommandTextArea/caretPosition.ts
const MIRROR_STYLES = [
	"boxSizing",
	"width",
	"height",
	"overflowX",
	"overflowY",
	"paddingTop",
	"paddingRight",
	"paddingBottom",
	"paddingLeft",
	"borderTopWidth",
	"borderRightWidth",
	"borderBottomWidth",
	"borderLeftWidth",
	"fontStyle",
	"fontVariant",
	"fontWeight",
	"fontStretch",
	"fontSize",
	"fontSizeAdjust",
	"lineHeight",
	"fontFamily",
	"textAlign",
	"textTransform",
	"textIndent",
	"textDecoration",
	"letterSpacing",
	"wordSpacing",
	"tabSize",
	"whiteSpace",
	"wordWrap",
	"wordBreak",
	"overflowWrap"
];
/**
* Returns the viewport-space rect of the caret at `index` within `textarea`,
* or `null` if it can't be measured (e.g. jsdom, no layout, detached node).
*/
function getCaretRect(textarea, index) {
	if (typeof document === "undefined") return null;
	const doc = textarea.ownerDocument;
	const style = doc.defaultView?.getComputedStyle(textarea);
	if (!style) return null;
	const box = textarea.getBoundingClientRect();
	if (box.width === 0 && box.height === 0) return null;
	const mirror = doc.createElement("div");
	mirror.setAttribute("data-caret-mirror", "");
	mirror.style.position = "absolute";
	mirror.style.visibility = "hidden";
	mirror.style.whiteSpace = "pre-wrap";
	mirror.style.wordWrap = "break-word";
	mirror.style.top = "0";
	mirror.style.left = "0";
	mirror.style.overflow = "hidden";
	for (const prop of MIRROR_STYLES) {
		const value = style[prop];
		if (value != null) mirror.style.setProperty(prop.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()), value);
	}
	mirror.style.width = style.width;
	const value = textarea.value;
	const before = value.slice(0, index);
	const after = value.slice(index) || ".";
	mirror.textContent = before;
	const marker = doc.createElement("span");
	marker.textContent = after;
	marker.style.display = "inline-block";
	mirror.appendChild(marker);
	doc.body.appendChild(mirror);
	let rect = null;
	try {
		const mirrorRect = mirror.getBoundingClientRect();
		const markerRect = marker.getBoundingClientRect();
		const { scrollTop, scrollLeft } = textarea;
		rect = {
			left: box.left - scrollLeft + (markerRect.left - mirrorRect.left),
			top: box.top - scrollTop + (markerRect.top - mirrorRect.top),
			height: markerRect.height || parseFloat(style.lineHeight) || 0
		};
	} finally {
		mirror.remove();
	}
	return rect.height > 0 ? rect : null;
}

//#endregion
export { getCaretRect };
//# sourceMappingURL=caretPosition.js.map