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 { useState, useLayoutEffect } from 'react';

/**
 * useElementSize Hook 配置
 */

/**
 * 元素尺寸信息
 */

/**
 * 元素尺寸计算 Hook（宽度和高度）
 *
 * @description 计算指定元素的宽度和高度，并监听尺寸变化自动更新
 * 使用 ResizeObserver 和防抖机制优化性能，减少不必要的更新
 *
 * @example
 * const { width, height } = useElementSize({
 *   selector: '.modal-container',
 *   defaultWidth: 0,
 *   defaultHeight: 0
 * });
 */
export var useElementSize = function useElementSize(_ref) {
  var selector = _ref.selector,
    _ref$enabled = _ref.enabled,
    enabled = _ref$enabled === void 0 ? true : _ref$enabled,
    _ref$defaultWidth = _ref.defaultWidth,
    defaultWidth = _ref$defaultWidth === void 0 ? 0 : _ref$defaultWidth,
    _ref$defaultHeight = _ref.defaultHeight,
    defaultHeight = _ref$defaultHeight === void 0 ? 0 : _ref$defaultHeight,
    _ref$debounceMs = _ref.debounceMs,
    debounceMs = _ref$debounceMs === void 0 ? 100 : _ref$debounceMs;
  var _useState = useState({
      width: defaultWidth,
      height: defaultHeight
    }),
    _useState2 = _slicedToArray(_useState, 2),
    size = _useState2[0],
    setSize = _useState2[1];
  var handScale = function handScale() {
    var _getComputedStyle;
    var bodyTransform = (_getComputedStyle = getComputedStyle(document.body)) === null || _getComputedStyle === void 0 ? void 0 : _getComputedStyle.transform;
    var scaleY = 1;
    var scaleX = 1;
    if (bodyTransform !== 'none') {
      var matrix = new DOMMatrix(bodyTransform);
      scaleX = matrix.a;
      scaleY = matrix.d;
    }
    return {
      scaleX: scaleX,
      scaleY: scaleY
    };
  };
  useLayoutEffect(function () {
    if (!enabled || typeof window === 'undefined' || typeof document === 'undefined') {
      return;
    }

    // 如果选择器为空，使用默认尺寸
    if (!selector || !selector.trim()) {
      setSize({
        width: defaultWidth,
        height: defaultHeight
      });
      return;
    }

    // 监听窗口大小变化
    var handleResize = function handleResize() {
      setTimeout(function () {
        var element = null;
        try {
          var _element;
          element = document.querySelector(selector);
          var rect = (_element = element) === null || _element === void 0 ? void 0 : _element.getBoundingClientRect();
          if (rect) {
            var _element2;
            var _handScale = handScale(),
              scaleX = _handScale.scaleX;
            var elementWidth = ((_element2 = element) === null || _element2 === void 0 ? void 0 : _element2.clientWidth) || 0;
            var scaledWidth = elementWidth * scaleX;
            var width = Math.round(scaledWidth * 100) / 100;
            var height = Math.round((rect === null || rect === void 0 ? void 0 : rect.height) * 100) / 100;
            setSize({
              width: width,
              height: height
            });
          }
        } catch (error) {
          setSize({
            width: defaultWidth,
            height: defaultHeight
          });
          return;
        }
      }, debounceMs);
    };
    handleResize();
    window.addEventListener('resize', handleResize);
    return function () {
      window.removeEventListener('resize', handleResize);
    };
  }, [selector, enabled, defaultWidth, defaultHeight, debounceMs]);
  return size;
};