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

/**
 * 可拖拽两栏（左结果 / 右详情）宽度的 hook。
 *
 * 设计要点：
 * - **按「占比」而非绝对像素**存储右栏宽度。低代码宿主会给 `document.body`
 *   施加 `transform: scale(...)`，鼠标 `clientX` 与元素 `getBoundingClientRect`
 *   都在同一「视觉坐标系」里，二者相除得到的占比与缩放无关，天然抵消缩放影响；
 * - 拖拽期间在 body 上临时锁 `user-select` / `cursor`，避免选中文本、光标乱跳；
 * - 通过 min/max 占比夹取，避免某一栏被拖到 0。
 */

var clamp = function clamp(v, lo, hi) {
  return Math.min(hi, Math.max(lo, v));
};
export function useResizablePane(options) {
  var _ref = options || {},
    _ref$initialRatio = _ref.initialRatio,
    initialRatio = _ref$initialRatio === void 0 ? 0.34 : _ref$initialRatio,
    _ref$minRatio = _ref.minRatio,
    minRatio = _ref$minRatio === void 0 ? 0.24 : _ref$minRatio,
    _ref$maxRatio = _ref.maxRatio,
    maxRatio = _ref$maxRatio === void 0 ? 0.6 : _ref$maxRatio;
  var _useState = useState(function () {
      return clamp(initialRatio, minRatio, maxRatio);
    }),
    _useState2 = _slicedToArray(_useState, 2),
    ratio = _useState2[0],
    setRatio = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    resizing = _useState4[0],
    setResizing = _useState4[1];
  var containerRef = useRef(null);
  var onResizerMouseDown = useCallback(function (e) {
    e.preventDefault();
    e.stopPropagation();
    var container = containerRef.current;
    if (!container) return;
    setResizing(true);
    var prevUserSelect = document.body.style.userSelect;
    var prevCursor = document.body.style.cursor;
    document.body.style.userSelect = 'none';
    document.body.style.cursor = 'col-resize';
    var onMove = function onMove(ev) {
      var rect = container.getBoundingClientRect();
      if (rect.width <= 0) return;
      // 右栏宽度 = 容器右边界 - 鼠标位置；除以容器宽度得到占比（缩放自动抵消）
      var next = clamp((rect.right - ev.clientX) / rect.width, minRatio, maxRatio);
      setRatio(next);
    };
    var onUp = function onUp() {
      setResizing(false);
      document.removeEventListener('mousemove', onMove);
      document.removeEventListener('mouseup', onUp);
      document.body.style.userSelect = prevUserSelect;
      document.body.style.cursor = prevCursor;
    };
    document.addEventListener('mousemove', onMove);
    document.addEventListener('mouseup', onUp);
  }, [minRatio, maxRatio]);
  return {
    ratio: ratio,
    rightWidth: "".concat((ratio * 100).toFixed(2), "%"),
    containerRef: containerRef,
    onResizerMouseDown: onResizerMouseDown,
    resizing: resizing
  };
}