import { forwardRef, useContext, useRef, useState, useEffect, useImperativeHandle } from "react";
import { ConfigContext } from "../ConfigProvider/index.js";
import cs from "../_util/classNames.js";
import { isObject, isUndefined, isNumber, isString, isFunction } from "../_util/is.js";
import ResizeTrigger from "./resize-trigger.js";
import { on, off } from "../_util/dom.js";
import omit from "../_util/omit.js";
import { jsx, jsxs, Fragment } 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 __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var DIRECTION_HORIZONTAL = "horizontal";
var DIRECTION_VERTICAL = "vertical";
function SplitGroup(props, ref) {
  var _a, _b;
  var panes = props.panes, style = props.style, className = props.className, _c = props.component, component = _c === void 0 ? "div" : _c, _d = props.direction, direction = _d === void 0 ? "horizontal" : _d, icon = props.icon, rest = __rest(props, ["panes", "style", "className", "component", "direction", "icon"]);
  var _e = useContext(ConfigContext), getPrefixCls = _e.getPrefixCls, rtl = _e.rtl;
  var defaultOffset = 1 / panes.length;
  var wrapperRef = useRef();
  var recordRef = useRef(new Array(panes.length).fill({
    moving: false,
    startOffset: 0,
    startPosition: 0
  }));
  var paneContainers = useRef([]);
  var movingIndex = useRef(0);
  var prevOffsets = useRef([]);
  var _f = __read(useState(new Array(panes.length).fill(defaultOffset)), 2), offsets = _f[0], setOffsets = _f[1];
  var _g = __read(useState(false), 2), isMoving = _g[0], setIsMoving = _g[1];
  var _h = __read(useState(new Array(panes.length).fill(0)), 2), triggerSize = _h[0], setTriggerSize = _h[1];
  var _j = __read(useState(new Array(Math.max(panes.length - 1, 0)).fill({
    prev: false,
    next: false
  })), 2), collapsedStatus = _j[0], setCollapsedStatus = _j[1];
  var prefixCls = getPrefixCls("resizebox-split-group");
  var isHorizontal = direction === DIRECTION_HORIZONTAL;
  var rtlReverse = isHorizontal && rtl;
  var isTriggerHorizontal = !isHorizontal;
  var classNames = cs(prefixCls, prefixCls + "-" + (isHorizontal ? DIRECTION_HORIZONTAL : DIRECTION_VERTICAL), (_a = {}, _a[prefixCls + "-moving"] = isMoving, _a), (_b = {}, _b[prefixCls + "-rtl"] = rtl, _b), className);
  var Tag = component;
  var getInitialOffsets = function() {
    var newOffsets = [];
    panes.forEach(function(pane) {
      var size = pane.size;
      if (!isUndefined(size)) {
        newOffsets.push(formatSize(size));
      } else {
        newOffsets.push(void 0);
      }
    });
    var noSizeArr = newOffsets.filter(function(size) {
      return !size;
    });
    var remainPercent = 1 - newOffsets.reduce(function(a, b) {
      var formatA = a || 0;
      var formatB = b || 0;
      return formatA + formatB;
    }, 0);
    var averagePercent = remainPercent / noSizeArr.length;
    newOffsets = newOffsets.map(function(size) {
      if (!isUndefined(size)) {
        return size;
      }
      return averagePercent;
    });
    return newOffsets;
  };
  var getPaneSize = function(index) {
    var prevTriggerSize = triggerSize[index - 1] || 0;
    var currentTriggerSize = triggerSize[index];
    var baseVal = offsets[index] * 100;
    var unit = "%";
    return "calc(" + baseVal + unit + " - " + (prevTriggerSize + currentTriggerSize) / 2 + "px)";
  };
  function formatSize(size) {
    var totalPX = isHorizontal ? wrapperRef.current.offsetWidth : wrapperRef.current.offsetHeight;
    if (!size || isNumber(size) && size < 0) {
      return 0;
    }
    var percent = isString(size) ? parseFloat(size) / totalPX : size;
    return Math.min(percent, 1);
  }
  var getMinAndMax = function(index) {
    var next = Math.min(index + 1, panes.length - 1);
    var totalOffset = offsets[index] + offsets[next];
    var currentMin = formatSize(panes[index].min) || 0;
    var currentMax = formatSize(panes[index].max) || totalOffset;
    var nextMin = formatSize(panes[next].min) || 0;
    var nextMax = formatSize(panes[next].max) || totalOffset;
    currentMax = Math.min(totalOffset - nextMin, currentMax);
    nextMax = Math.min(totalOffset - currentMin, nextMax);
    return {
      currentMin,
      currentMax,
      nextMin,
      nextMax
    };
  };
  var getNewOffsets = function(startOffset, startPosition, currentPosition) {
    var current = movingIndex.current;
    var next = current + 1;
    var newOffsets = __spreadArray([], __read(offsets), false);
    var ratio = rtlReverse ? -1 : 1;
    var currentPercent = offsets[current];
    var nextPercent = offsets[next];
    var totalPercent = currentPercent + nextPercent;
    var _a2 = getMinAndMax(current), minOffset = _a2.currentMin, maxOffset = _a2.currentMax;
    var moveOffset = startOffset + formatSize((currentPosition - startPosition) * ratio + "px");
    moveOffset = Math.max(minOffset, moveOffset);
    moveOffset = Math.min(maxOffset, moveOffset);
    newOffsets[current] = moveOffset;
    newOffsets[next] = totalPercent - moveOffset;
    return newOffsets;
  };
  function onTriggerResize(e, index) {
    var contentRect = e[0].contentRect;
    var currentSize = contentRect[isTriggerHorizontal ? "height" : "width"];
    var newTriggerSize = __spreadArray([], __read(triggerSize), false);
    newTriggerSize[index] = currentSize;
    setTriggerSize(newTriggerSize);
  }
  var getCollapsedConfig = function(index) {
    var collapsible = panes[index].collapsible;
    if (!isObject(collapsible)) {
      collapsible = !collapsible ? {} : {
        prev: true,
        next: true
      };
    }
    var prev = collapsible.prev, next = collapsible.next;
    if (!prev && !next) {
      return {};
    }
    if (!collapsedStatus[index]) {
      return {};
    }
    var hasPrev = !!prev || !prev && collapsedStatus[index].next;
    var hasNext = !!next || !next && collapsedStatus[index].prev;
    return {
      hasPrev,
      hasNext
    };
  };
  function onTriggerMouseDown(e, index) {
    props.onMovingStart && props.onMovingStart(index);
    movingIndex.current = index;
    var currentRecord = recordRef.current[index];
    currentRecord.moving = true;
    currentRecord.startOffset = offsets[index];
    currentRecord.startPosition = isHorizontal ? e.pageX : e.pageY;
    setIsMoving(true);
    on(window, "mousemove", moving);
    on(window, "touchmove", moving);
    on(window, "mouseup", moveEnd);
    on(window, "touchend", moveEnd);
    on(window, "contextmenu", moveEnd);
    document.body.style.cursor = isTriggerHorizontal ? "row-resize" : "col-resize";
  }
  function moving(e) {
    var index = movingIndex.current;
    var currentRecord = recordRef.current[index];
    var totalPX = isHorizontal ? wrapperRef.current.offsetWidth : wrapperRef.current.offsetHeight;
    if (currentRecord.moving) {
      var newOffsets = getNewOffsets(currentRecord.startOffset, currentRecord.startPosition, isHorizontal ? e.pageX : e.pageY);
      setOffsets(newOffsets);
      prevOffsets.current = newOffsets;
      props.onMoving && props.onMoving(e, newOffsets.map(function(value) {
        return value * totalPX + "px";
      }), index);
    }
  }
  function moveEnd() {
    var index = movingIndex.current;
    recordRef.current[index].moving = false;
    setIsMoving(false);
    off(window, "mousemove", moving);
    off(window, "touchmove", moving);
    off(window, "mouseup", moveEnd);
    off(window, "touchend", moveEnd);
    off(window, "contextmenu", moveEnd);
    document.body.style.cursor = "default";
    props.onMovingEnd && props.onMovingEnd(index);
  }
  function handleCollapsed(e, index, status, callback) {
    var next = index + 1;
    var newOffset = __spreadArray([], __read(offsets), false);
    var currentOffset = offsets[index];
    var nextOffset = offsets[next];
    var totalOffset = currentOffset + nextOffset;
    var totalPX = isHorizontal ? wrapperRef.current.offsetWidth : wrapperRef.current.offsetHeight;
    var _a2 = getMinAndMax(index), currentMin = _a2.currentMin, nextMin = _a2.nextMin;
    var newCurrentOffset = prevOffsets.current[index];
    var newNextOffset = prevOffsets.current[next];
    var collapsed = collapsedStatus[index][status];
    if (status === "prev") {
      if (nextOffset !== nextMin || newNextOffset === nextMin) {
        newCurrentOffset = currentMin;
        newNextOffset = totalOffset - currentMin;
        collapsed = true;
      }
    } else if (currentOffset !== currentMin || newCurrentOffset === currentMin) {
      newCurrentOffset = totalOffset - nextMin;
      newNextOffset = nextMin;
      collapsed = true;
    }
    newOffset[index] = newCurrentOffset;
    newOffset[next] = newNextOffset;
    props.onMoving && props.onMoving(e, newOffset.map(function(value) {
      return value * totalPX + "px";
    }), index);
    props.onMovingEnd && props.onMovingEnd(index);
    setOffsets(newOffset);
    if (isFunction(callback)) {
      callback(e, index, status, collapsed);
    }
  }
  useEffect(function() {
    var offsets2 = getInitialOffsets();
    setOffsets(offsets2);
    prevOffsets.current = offsets2;
  }, [JSON.stringify(panes.map(function(item) {
    return item.size;
  }))]);
  useImperativeHandle(ref, function() {
    return wrapperRef.current;
  }, []);
  useEffect(function() {
    var newCollapsedStatus = [];
    offsets.forEach(function(offset, index) {
      var currentCollapsedStatus = {
        prev: false,
        next: false
      };
      var next = index + 1;
      var _a2 = getMinAndMax(index), currentMin = _a2.currentMin, nextMin = _a2.nextMin;
      if (offset === currentMin) {
        currentCollapsedStatus.prev = true;
      } else if (offsets[next] === nextMin) {
        currentCollapsedStatus.next = true;
      }
      newCollapsedStatus.push(currentCollapsedStatus);
    });
    setCollapsedStatus(newCollapsedStatus);
  }, [offsets]);
  return /* @__PURE__ */ jsx(Tag, {
    ...__assign({}, omit(rest, ["onMovingStart", "onPaneResize", "onMoving", "onMovingEnd"]), {
      style,
      className: classNames,
      ref: wrapperRef
    }),
    children: panes.map(function(pane, index) {
      var content = pane.content, disabled = pane.disabled, trigger = pane.trigger, _a2 = pane.resizable, resizable = _a2 === void 0 ? true : _a2, _b2 = pane.collapsible, collapsible = _b2 === void 0 ? {} : _b2;
      var _c2 = getCollapsedConfig(index), hasPrev = _c2.hasPrev, hasNext = _c2.hasNext;
      var prevConfig = isObject(collapsible) && isObject(collapsible.prev) ? collapsible.prev : {};
      var nextConfig = isObject(collapsible) && isObject(collapsible.next) ? collapsible.next : {};
      return /* @__PURE__ */ jsxs(Fragment, {
        children: [/* @__PURE__ */ jsx("div", {
          className: prefixCls + "-pane",
          style: {
            flexBasis: getPaneSize(index)
          },
          ref: function(el) {
            return paneContainers.current[index] = el;
          },
          children: content
        }), !disabled && index !== panes.length - 1 && /* @__PURE__ */ jsx(ResizeTrigger, {
          className: prefixCls + "-trigger",
          direction: isTriggerHorizontal ? DIRECTION_HORIZONTAL : DIRECTION_VERTICAL,
          icon,
          onResize: function(e) {
            return onTriggerResize(e, index);
          },
          onMouseDown: function(e) {
            return onTriggerMouseDown(e, index);
          },
          collapsible: {
            prev: hasPrev ? {
              onClick: function(e) {
                return handleCollapsed(e, index, "prev", prevConfig.onClick);
              },
              icon: prevConfig.icon,
              collapsed: collapsedStatus[index].prev
            } : void 0,
            next: hasNext ? {
              onClick: function(e) {
                return handleCollapsed(e, index, "next", nextConfig.onClick);
              },
              icon: nextConfig.icon,
              collapsed: collapsedStatus[index].next
            } : void 0
          },
          resizable,
          renderChildren: trigger
        })]
      });
    })
  });
}
var SplitGroupComponent = forwardRef(SplitGroup);
SplitGroupComponent.displayName = "ResizeBoxSplitGroup";
const SplitGroup$1 = SplitGroupComponent;
export {
  SplitGroup$1 as default
};
//# sourceMappingURL=split-group.js.map
