"use strict";
const React = require("react");
const classNames = require("../_util/classNames.js");
const resizeTrigger = require("./resize-trigger.js");
const index = require("../ConfigProvider/index.js");
const dom = require("../_util/dom.js");
const useIsomorphicLayoutEffect = require("../_util/hooks/useIsomorphicLayoutEffect.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var DIRECTION_HORIZONTAL = "horizontal";
var DIRECTION_VERTICAL = "vertical";
function Split(props, ref) {
  var _a;
  var style = props.style, className = props.className, _b = props.component, component = _b === void 0 ? "div" : _b, _c = props.direction, direction = _c === void 0 ? "horizontal" : _c, icon = props.icon, _d = props.size, size = _d === void 0 ? 0.5 : _d, min = props.min, max = props.max, panes = props.panes, disabled = props.disabled, trigger = props.trigger;
  var _e = React.useContext(index.ConfigContext), getPrefixCls = _e.getPrefixCls, rtl = _e.rtl;
  var prefixCls = getPrefixCls("resizebox-split");
  var isHorizontal = direction.includes(DIRECTION_HORIZONTAL);
  var isReverse = direction.includes("reverse");
  var rtlReverse = isHorizontal && rtl;
  var isTriggerHorizontal = !isHorizontal;
  var classNames$1 = classNames(prefixCls, prefixCls + "-" + (isHorizontal ? DIRECTION_HORIZONTAL : DIRECTION_VERTICAL), (_a = {}, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var _f = __read(panes, 2), firstPane = _f[0], secondPane = _f[1];
  var isPxSize = typeof size === "string";
  var _g = __read(React.useState(parseFloat(size)), 2), offset = _g[0], setOffset = _g[1];
  var _h = __read(React.useState(0), 2), triggerSize = _h[0], setTriggerSize = _h[1];
  var recordRef = React.useRef({
    startX: 0,
    startY: 0,
    startWidth: 0,
    startHeight: 0,
    startOffset: 0,
    moving: false
  });
  var wrapperRef = React.useRef();
  var paneContainers = React.useRef([]);
  React.useImperativeHandle(ref, function() {
    return wrapperRef.current;
  }, []);
  function px2percent(numerator, denominator) {
    return parseFloat(numerator) / parseFloat(denominator);
  }
  function getOffset(startSize, startOffset, startPosition, currentPosition) {
    var minOffset = min ? parseFloat(min) : 0;
    var maxOffset = max ? parseFloat(max) : isPxSize ? startSize : 1;
    var ratio = isReverse ? -1 : 1;
    var rtlRatio = rtlReverse ? -1 : 1;
    ratio *= rtlRatio;
    var moveOffset = isPxSize ? startOffset + (currentPosition - startPosition) * ratio : px2percent(startSize * startOffset + (currentPosition - startPosition) * ratio, startSize);
    moveOffset = Math.max(moveOffset, minOffset);
    moveOffset = Math.min(moveOffset, maxOffset);
    return moveOffset;
  }
  function onTriggerMouseDown(e) {
    var _a2, _b2;
    props.onMovingStart && props.onMovingStart();
    recordRef.current.moving = true;
    recordRef.current.startX = e.pageX;
    recordRef.current.startY = e.pageY;
    recordRef.current.startWidth = (_a2 = wrapperRef.current) === null || _a2 === void 0 ? void 0 : _a2.offsetWidth;
    recordRef.current.startHeight = (_b2 = wrapperRef.current) === null || _b2 === void 0 ? void 0 : _b2.offsetHeight;
    recordRef.current.startOffset = offset;
    dom.on(window, "mousemove", moving);
    dom.on(window, "touchmove", moving);
    dom.on(window, "mouseup", moveEnd);
    dom.on(window, "touchend", moveEnd);
    dom.on(window, "contextmenu", moveEnd);
    document.body.style.cursor = isTriggerHorizontal ? "row-resize" : "col-resize";
  }
  function moving(e) {
    if (recordRef.current.moving) {
      var newOffset = isHorizontal ? getOffset(recordRef.current.startWidth, recordRef.current.startOffset, recordRef.current.startX, e.pageX) : getOffset(recordRef.current.startHeight, recordRef.current.startOffset, recordRef.current.startY, e.pageY);
      setOffset(newOffset);
      props.onMoving && props.onMoving(e, isPxSize ? newOffset + "px" : newOffset);
    }
  }
  function moveEnd() {
    recordRef.current.moving = false;
    dom.off(window, "mousemove", moving);
    dom.off(window, "touchmove", moving);
    dom.off(window, "mouseup", moveEnd);
    dom.off(window, "touchend", moveEnd);
    dom.off(window, "contextmenu", moveEnd);
    document.body.style.cursor = "default";
    props.onMovingEnd && props.onMovingEnd();
  }
  function onTriggerResize(e) {
    var contentRect = e[0].contentRect;
    var newTriggerSize = contentRect[isTriggerHorizontal ? "height" : "width"];
    setTriggerSize(newTriggerSize);
  }
  function getFirstPaneSize() {
    var unit = isPxSize ? "px" : "%";
    if (!offset)
      return "0" + unit;
    var baseVal = isPxSize ? offset : offset * 100;
    return "calc(" + baseVal + unit + " - " + triggerSize / 2 + "px)";
  }
  React.useEffect(function() {
    props.onPaneResize && props.onPaneResize(paneContainers.current);
  }, [offset, triggerSize]);
  useIsomorphicLayoutEffect(function() {
    var newOffset = parseFloat(size);
    if (offset !== newOffset) {
      setOffset(newOffset);
    }
  }, [size]);
  var Tag = component;
  var firstPaneNode = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: classNames(prefixCls + "-pane", "first-pane"),
    style: {
      flexBasis: getFirstPaneSize()
    },
    ref: function(el) {
      paneContainers.current[0] = el;
    },
    children: firstPane
  });
  var secondPaneNode = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: classNames(prefixCls + "-pane", "second-pane"),
    ref: function(el) {
      paneContainers.current[1] = el;
    },
    children: secondPane
  });
  var paneNodeArr = isReverse ? [secondPaneNode, firstPaneNode] : [firstPaneNode, secondPaneNode];
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(Tag, {
    style,
    className: classNames$1,
    ref: wrapperRef,
    children: [paneNodeArr[0], !disabled && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(resizeTrigger, {
      className: prefixCls + "-trigger",
      direction: isTriggerHorizontal ? DIRECTION_HORIZONTAL : DIRECTION_VERTICAL,
      icon,
      onMouseDown: onTriggerMouseDown,
      onResize: onTriggerResize,
      children: trigger
    }), paneNodeArr[1]]
  });
}
var SplitComponent = React.forwardRef(Split);
SplitComponent.displayName = "ResizeBoxSplit";
const Split$1 = SplitComponent;
module.exports = Split$1;
//# sourceMappingURL=split.js.map
