function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useCallback, useEffect, useRef, forwardRef, useImperativeHandle } from 'react';
import classNames from 'classnames';
import AutoResizeText from "../../auto-resize-text";
import "./index.less";
var BaseVirtualInput = /*#__PURE__*/forwardRef(function (props, ref) {
  var _props$value = props.value,
    value = _props$value === void 0 ? '' : _props$value,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '' : _props$placeholder,
    _props$autoFocus = props.autoFocus,
    autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    _props$height = props.height,
    height = _props$height === void 0 ? 60 : _props$height,
    fontSize = props.fontSize,
    onFocus = props.onFocus,
    onBlur = props.onBlur,
    style = props.style,
    className = props.className,
    _props$preventBlurEle = props.preventBlurElements,
    preventBlurElements = _props$preventBlurEle === void 0 ? [] : _props$preventBlurEle,
    focused = props.focused,
    _props$showCursor = props.showCursor,
    showCursor = _props$showCursor === void 0 ? true : _props$showCursor,
    _props$selected = props.selected,
    selected = _props$selected === void 0 ? false : _props$selected;
  // 使用 useControllableValue 管理 focused 状态
  var _useState = useState(focused !== null && focused !== void 0 ? focused : autoFocus),
    _useState2 = _slicedToArray(_useState, 2),
    localFocused = _useState2[0],
    setLocalFocused = _useState2[1];
  var internalFocused = 'focused' in props ? props.focused : localFocused;
  var containerRef = useRef(null);

  // 处理聚焦
  var handleFocus = useCallback(function () {
    if (disabled) return;
    setLocalFocused(true);
    onFocus === null || onFocus === void 0 || onFocus();
  }, [disabled, onFocus]);

  // 处理失焦
  var handleBlur = useCallback(function () {
    setLocalFocused(false);
    onBlur === null || onBlur === void 0 || onBlur();
  }, [onBlur]);
  var shouldPreventBlur = useCallback(function (target) {
    if (!target || !(target instanceof Element)) return false;
    return preventBlurElements.some(function (element) {
      if (typeof element === 'string') {
        // 字符串选择器 - 查找页面中所有匹配的元素，检查是否包含目标元素
        var matchedElements = document.querySelectorAll(element);
        return Array.from(matchedElements).some(function (el) {
          return el.contains(target) || el === target;
        });
      } else if (element && 'current' in element && element.current) {
        // React ref
        return element.current.contains(target);
      }
      return false;
    });
  }, [preventBlurElements]);
  useEffect(function () {
    var handleGlobalClick = function handleGlobalClick(event) {
      if (!internalFocused) return;
      var target = event.target;
      if (containerRef.current && containerRef.current.contains(target)) {
        return;
      }
      if (shouldPreventBlur(target)) {
        return;
      }
      handleBlur();
    };
    if (internalFocused) {
      document.addEventListener('mousedown', handleGlobalClick);
    }
    return function () {
      document.removeEventListener('mousedown', handleGlobalClick);
    };
  }, [internalFocused, handleBlur, shouldPreventBlur]);

  // 处理点击事件
  var handleClick = useCallback(function (event) {
    handleFocus();
  }, [handleFocus]);

  // 暴露给父组件的方法
  useImperativeHandle(ref, function () {
    return {
      focus: handleFocus,
      blur: handleBlur
    };
  }, [handleFocus, handleBlur]);

  // 计算组件高度
  var componentHeight = typeof height === 'number' ? "".concat(height, "px") : height;

  // 获取基础字体大小
  var getBaseFontSize = useCallback(function () {
    if (fontSize) {
      return typeof fontSize === 'number' ? fontSize : parseFloat(fontSize);
    }
    return typeof height === 'number' ? height * 0.8 : 48;
  }, [fontSize, height]);

  // 计算最小和最大字体大小
  var baseFontSize = getBaseFontSize();
  var minFontSize = Math.max(10, baseFontSize * 0.2);
  var maxFontSize = baseFontSize;

  // 计算光标高度（容器高度的三分之二）
  var caretHeight = "calc(".concat(componentHeight, " * 0.67)");

  // 合并样式，将高度和光标高度传递给组件
  var mergedStyle = _objectSpread(_objectSpread({}, style), {}, {
    '--virtual-input-height': componentHeight,
    '--virtual-input-caret-height': caretHeight,
    '--virtual-input-font-size': "".concat(baseFontSize, "px")
  });

  // 是否显示占位符
  var showPlaceholder = value === undefined || value === '';

  // 选中状态下不显示光标
  var shouldShowCursor = internalFocused && !disabled && showCursor && !selected;
  return /*#__PURE__*/React.createElement("div", {
    ref: containerRef,
    className: classNames('pisell-base-virtual-input', "pisell-base-virtual-input--".concat(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.createElement(AutoResizeText, {
    className: classNames('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: 0.5
  }, placeholder), !!value && /*#__PURE__*/React.createElement(AutoResizeText, {
    className: classNames('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__*/React.createElement("div", {
    className: "pisell-base-virtual-input-caret-container"
  }, shouldShowCursor && /*#__PURE__*/React.createElement("div", {
    className: "pisell-base-virtual-input-caret"
  })));
});
BaseVirtualInput.displayName = 'BaseVirtualInput';
export default BaseVirtualInput;