const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../auto-resize-text/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("../../../../../../../materials/src/components/virtualInput/BaseVirtualInput/index.less");
//#region ../materials/src/components/virtualInput/BaseVirtualInput/index.tsx
const BaseVirtualInput = (0, react.forwardRef)((props, ref) => {
	const { value = "", placeholder = "", autoFocus = false, disabled = false, height = 60, fontSize, onFocus, onBlur, style, className, preventBlurElements = [], focused, showCursor = true, selected = false } = props;
	const [localFocused, setLocalFocused] = (0, react.useState)(focused !== null && focused !== void 0 ? focused : autoFocus);
	const internalFocused = "focused" in props ? props.focused : localFocused;
	const containerRef = (0, react.useRef)(null);
	const handleFocus = (0, react.useCallback)(() => {
		if (disabled) return;
		setLocalFocused(true);
		onFocus === null || onFocus === void 0 || onFocus();
	}, [disabled, onFocus]);
	const handleBlur = (0, react.useCallback)(() => {
		setLocalFocused(false);
		onBlur === null || onBlur === void 0 || onBlur();
	}, [onBlur]);
	const shouldPreventBlur = (0, react.useCallback)((target) => {
		if (!target || !(target instanceof Element)) return false;
		return preventBlurElements.some((element) => {
			if (typeof element === "string") {
				const matchedElements = document.querySelectorAll(element);
				return Array.from(matchedElements).some((el) => el.contains(target) || el === target);
			} else if (element && "current" in element && element.current) return element.current.contains(target);
			return false;
		});
	}, [preventBlurElements]);
	(0, react.useEffect)(() => {
		const handleGlobalClick = (event) => {
			if (!internalFocused) return;
			const target = event.target;
			if (containerRef.current && containerRef.current.contains(target)) return;
			if (shouldPreventBlur(target)) return;
			handleBlur();
		};
		if (internalFocused) document.addEventListener("mousedown", handleGlobalClick);
		return () => {
			document.removeEventListener("mousedown", handleGlobalClick);
		};
	}, [
		internalFocused,
		handleBlur,
		shouldPreventBlur
	]);
	const handleClick = (0, react.useCallback)((event) => {
		handleFocus();
	}, [handleFocus]);
	(0, react.useImperativeHandle)(ref, () => ({
		focus: handleFocus,
		blur: handleBlur
	}), [handleFocus, handleBlur]);
	const componentHeight = typeof height === "number" ? `${height}px` : height;
	const baseFontSize = (0, react.useCallback)(() => {
		if (fontSize) return typeof fontSize === "number" ? fontSize : parseFloat(fontSize);
		return typeof height === "number" ? height * .8 : 48;
	}, [fontSize, height])();
	const minFontSize = Math.max(10, baseFontSize * .2);
	const maxFontSize = baseFontSize;
	const caretHeight = `calc(${componentHeight} * 0.67)`;
	const mergedStyle = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, {
		"--virtual-input-height": componentHeight,
		"--virtual-input-caret-height": caretHeight,
		"--virtual-input-font-size": `${baseFontSize}px`
	});
	const showPlaceholder = value === void 0 || value === "";
	const shouldShowCursor = internalFocused && !disabled && showCursor && !selected;
	return /* @__PURE__ */ react.default.createElement("div", {
		ref: containerRef,
		className: (0, classnames.default)("pisell-base-virtual-input", `pisell-base-virtual-input--${props === null || props === void 0 ? void 0 : props.textAlign}`, {
			"pisell-base-virtual-input--focused": internalFocused,
			"pisell-base-virtual-input--disabled": disabled,
			"pisell-base-virtual-input--selected": selected && !!value
		}, className),
		style: mergedStyle,
		onClick: handleClick
	}, showPlaceholder && /* @__PURE__ */ react.default.createElement(require_index.default, {
		className: (0, classnames.default)("pisell-base-virtual-input-placeholder", {
			"pisell-base-virtual-input-placeholder--end": (props === null || props === void 0 ? void 0 : props.textAlign) === "end",
			"pisell-base-virtual-input-placeholder--center": (props === null || props === void 0 ? void 0 : props.textAlign) === "center"
		}),
		mode: "oneline",
		minFontSizePx: minFontSize,
		maxFontSizePx: maxFontSize,
		fontSizePrecisionPx: .5
	}, placeholder), !!value && /* @__PURE__ */ react.default.createElement(require_index.default, {
		className: (0, classnames.default)("pisell-base-virtual-input-value", { "pisell-base-virtual-input-value--selected": selected }),
		mode: "oneline",
		minFontSizePx: minFontSize,
		maxFontSizePx: maxFontSize,
		fontSizePrecisionPx: .5,
		scrollToEnd: true
	}, value), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-base-virtual-input-caret-container" }, shouldShowCursor && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-base-virtual-input-caret" })));
});
BaseVirtualInput.displayName = "BaseVirtualInput";
//#endregion
exports.default = BaseVirtualInput;
