import { forwardRef, useContext, useState, useRef, useImperativeHandle } from "react";
import omit from "../_util/omit.js";
import cs from "../_util/classNames.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import { on, off } from "../_util/dom.js";
import ResizeTrigger from "./resize-trigger.js";
import Split from "./split.js";
import useMergeValue from "../_util/hooks/useMergeValue.js";
import { isNumber } from "../_util/is.js";
import useMergeProps from "../_util/hooks/useMergeProps.js";
import SplitGroup from "./split-group.js";
import { jsxs, jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
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_LEFT = "left";
var DIRECTION_RIGHT = "right";
var DIRECTION_TOP = "top";
var DIRECTION_BOTTOM = "bottom";
var allDirections = [DIRECTION_LEFT, DIRECTION_RIGHT, DIRECTION_TOP, DIRECTION_BOTTOM];
var defaultProps = {
  component: "div",
  directions: ["right"],
  resizeIcons: {},
  resizeTriggers: {}
};
var getOppositeDirection = function(direction) {
  switch (direction) {
    case "left":
      return "right";
    case "right":
      return "left";
    default:
      return direction;
  }
};
function ResizeBox(baseProps, ref) {
  var _a;
  var _b = useContext(ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.ResizeBox);
  var style = props.style, className = props.className, component = props.component, directions = props.directions, resizeIcons = props.resizeIcons, children = props.children, resizeTriggers = props.resizeTriggers, propWidth = props.width, propHeight = props.height, rest = __rest(props, ["style", "className", "component", "directions", "resizeIcons", "children", "resizeTriggers", "width", "height"]);
  var realDirections = rtl ? directions.map(function(dir) {
    return getOppositeDirection(dir);
  }) : directions;
  var prefixCls = getPrefixCls("resizebox");
  var classNames = cs(prefixCls, (_a = {}, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var _c = __read(useState({}), 2), paddingStyles = _c[0], setPaddingStyles = _c[1];
  var _d = __read(useMergeValue(void 0, {
    value: propWidth
  }), 2), width = _d[0], setWidth = _d[1];
  var _e = __read(useMergeValue(void 0, {
    value: propHeight
  }), 2), height = _e[0], setHeight = _e[1];
  var recordRef = useRef({
    startX: 0,
    startY: 0,
    startWidth: 0,
    startHeight: 0,
    direction: DIRECTION_RIGHT,
    moving: false,
    padding: {
      top: 0,
      bottom: 0,
      left: 0,
      right: 0
    }
  });
  var wrapperRef = useRef();
  useImperativeHandle(ref, function() {
    return wrapperRef.current;
  }, []);
  function getIsHorizontal(direction) {
    return [DIRECTION_TOP, DIRECTION_BOTTOM].indexOf(direction) > -1;
  }
  function getRealSize(clientSize, padding) {
    if (clientSize === 0)
      return 0;
    var res = clientSize - padding;
    return res <= 0 ? 0 : res;
  }
  function onTriggerMouseDown(direction, e) {
    var _a2, _b2;
    props.onMovingStart && props.onMovingStart();
    recordRef.current.moving = true;
    recordRef.current.startX = e.pageX;
    recordRef.current.startY = e.pageY;
    recordRef.current.direction = direction;
    var _c2 = recordRef.current.padding, top = _c2.top, left = _c2.left, right = _c2.right, bottom = _c2.bottom;
    recordRef.current.startWidth = getRealSize((_a2 = wrapperRef.current) === null || _a2 === void 0 ? void 0 : _a2.clientWidth, left + right);
    recordRef.current.startHeight = getRealSize((_b2 = wrapperRef.current) === null || _b2 === void 0 ? void 0 : _b2.clientHeight, top + bottom);
    on(window, "mousemove", moving);
    on(window, "touchmove", moving);
    on(window, "mouseup", moveEnd);
    on(window, "touchend", moveEnd);
    on(window, "contextmenu", moveEnd);
    document.body.style.cursor = getIsHorizontal(direction) ? "row-resize" : "col-resize";
  }
  function moving(e) {
    if (!recordRef.current.moving)
      return false;
    var _a2 = recordRef.current, startX = _a2.startX, startY = _a2.startY, startWidth = _a2.startWidth, startHeight = _a2.startHeight;
    var newWidth = startWidth;
    var newHeight = startHeight;
    var offsetX = e.pageX - startX;
    var offsetY = e.pageY - startY;
    switch (recordRef.current.direction) {
      case DIRECTION_LEFT:
        newWidth = startWidth - offsetX;
        setWidth(newWidth);
        break;
      case DIRECTION_RIGHT:
        newWidth = startWidth + offsetX;
        setWidth(newWidth);
        break;
      case DIRECTION_TOP:
        newHeight = startHeight - offsetY;
        setHeight(newHeight);
        break;
      case DIRECTION_BOTTOM:
        newHeight = startHeight + offsetY;
        setHeight(newHeight);
        break;
    }
    props.onMoving && props.onMoving(e, {
      width: newWidth,
      height: newHeight
    });
  }
  function moveEnd() {
    recordRef.current.moving = false;
    offEvents();
    document.body.style.cursor = "default";
    props.onMovingEnd && props.onMovingEnd();
  }
  function offEvents() {
    off(window, "mousemove", moving);
    off(window, "touchmove", moving);
    off(window, "mouseup", moveEnd);
    off(window, "touchend", moveEnd);
    off(window, "contextmenu", moveEnd);
  }
  function onTriggerResize(direction, e) {
    var isHorizontal = getIsHorizontal(direction);
    var contentRect = e[0].contentRect;
    var styleDirection = "" + direction.slice(0, 1).toUpperCase() + direction.slice(1);
    var size = contentRect[isHorizontal ? "height" : "width"];
    recordRef.current.padding[direction] = size;
    setPaddingStyles(function(pre) {
      var _a2;
      return __assign(__assign({}, pre), (_a2 = {}, _a2["padding" + styleDirection] = size, _a2));
    });
  }
  var wrapperStyles = __assign(__assign(__assign(__assign({}, paddingStyles), style || {}), isNumber(width) ? {
    width
  } : {}), isNumber(height) ? {
    height
  } : {});
  var Tag = component;
  return /* @__PURE__ */ jsxs(Tag, {
    ...__assign({}, omit(rest, ["onMovingStart", "onMoving", "onMovingEnd"]), {
      style: wrapperStyles,
      className: classNames,
      ref: wrapperRef
    }),
    children: [children, realDirections.map(function(direction) {
      if (allDirections.indexOf(direction) !== -1) {
        return /* @__PURE__ */ jsx(ResizeTrigger, {
          className: prefixCls + "-direction-" + direction,
          direction: getIsHorizontal(direction) ? "horizontal" : "vertical",
          icon: resizeIcons[direction],
          onMouseDown: function(e) {
            onTriggerMouseDown(direction, e);
          },
          onResize: function(e) {
            onTriggerResize(direction, e);
          },
          children: resizeTriggers[direction]
        }, direction);
      }
    })]
  });
}
var ForwardRefResizeBox = forwardRef(ResizeBox);
var ResizeBoxComponent = ForwardRefResizeBox;
ResizeBoxComponent.Split = Split;
ResizeBoxComponent.SplitGroup = SplitGroup;
ResizeBoxComponent.displayName = "ResizeBox";
const ResizeBox$1 = ResizeBoxComponent;
export {
  ResizeBox$1 as default
};
//# sourceMappingURL=index.js.map
