var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/virtualInput/BaseVirtualInput/index.tsx
var BaseVirtualInput_exports = {};
__export(BaseVirtualInput_exports, {
  default: () => BaseVirtualInput_default
});
module.exports = __toCommonJS(BaseVirtualInput_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_auto_resize_text = __toESM(require("../../auto-resize-text"));
var import_index = require("./index.less");
var BaseVirtualInput = (0, import_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, import_react.useState)(focused ?? autoFocus);
    const internalFocused = "focused" in props ? props.focused : localFocused;
    const containerRef = (0, import_react.useRef)(null);
    const handleFocus = (0, import_react.useCallback)(() => {
      if (disabled) return;
      setLocalFocused(true);
      onFocus == null ? void 0 : onFocus();
    }, [disabled, onFocus]);
    const handleBlur = (0, import_react.useCallback)(() => {
      setLocalFocused(false);
      onBlur == null ? void 0 : onBlur();
    }, [onBlur]);
    const shouldPreventBlur = (0, import_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, import_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, import_react.useCallback)(
      (event) => {
        handleFocus();
      },
      [handleFocus]
    );
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        focus: handleFocus,
        blur: handleBlur
      }),
      [handleFocus, handleBlur]
    );
    const componentHeight = typeof height === "number" ? `${height}px` : height;
    const getBaseFontSize = (0, import_react.useCallback)(() => {
      if (fontSize) {
        return typeof fontSize === "number" ? fontSize : parseFloat(fontSize);
      }
      return typeof height === "number" ? height * 0.8 : 48;
    }, [fontSize, height]);
    const baseFontSize = getBaseFontSize();
    const minFontSize = Math.max(10, baseFontSize * 0.2);
    const maxFontSize = baseFontSize;
    const caretHeight = `calc(${componentHeight} * 0.67)`;
    const mergedStyle = {
      ...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__ */ import_react.default.createElement(
      "div",
      {
        ref: containerRef,
        className: (0, import_classnames.default)(
          "pisell-base-virtual-input",
          `pisell-base-virtual-input--${props == null ? 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__ */ import_react.default.createElement(
        import_auto_resize_text.default,
        {
          className: (0, import_classnames.default)("pisell-base-virtual-input-placeholder", {
            "pisell-base-virtual-input-placeholder--end": (props == null ? void 0 : props.textAlign) === "end",
            "pisell-base-virtual-input-placeholder--center": (props == null ? void 0 : props.textAlign) === "center"
          }),
          mode: "oneline",
          minFontSizePx: minFontSize,
          maxFontSizePx: maxFontSize,
          fontSizePrecisionPx: 0.5
        },
        placeholder
      ),
      !!value && /* @__PURE__ */ import_react.default.createElement(
        import_auto_resize_text.default,
        {
          className: (0, import_classnames.default)("pisell-base-virtual-input-value", {
            "pisell-base-virtual-input-value--selected": selected
          }),
          mode: "oneline",
          minFontSizePx: minFontSize,
          maxFontSizePx: maxFontSize,
          fontSizePrecisionPx: 0.5,
          scrollToEnd: true
        },
        value
      ),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-base-virtual-input-caret-container" }, shouldShowCursor && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-base-virtual-input-caret" }))
    );
  }
);
BaseVirtualInput.displayName = "BaseVirtualInput";
var BaseVirtualInput_default = BaseVirtualInput;
