import React__default, { useContext, useState } from "react";
import { ConfigContext } from "../../ConfigProvider/index.js";
import cs from "../../_util/classNames.js";
import Item from "./item.js";
import { jsx } from "../../../../../../../@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;
};
function Draggable(props) {
  var getPrefixCls = useContext(ConfigContext).getPrefixCls;
  var prefixCls = getPrefixCls("draggable");
  var className = props.className, children = props.children, direction = props.direction, onIndexChange = props.onIndexChange, itemWrapperStyle = props.itemWrapperStyle;
  var _a = __read(useState(null), 2), dragItemIndex = _a[0], setDragItemIndex = _a[1];
  return /* @__PURE__ */ jsx("div", {
    className: cs(prefixCls, className),
    children: React__default.Children.map(children, function(child, index) {
      return /* @__PURE__ */ jsx(Item, {
        style: itemWrapperStyle,
        prefixCls,
        direction,
        onDragStart: function() {
          return setDragItemIndex(index);
        },
        onDragEnd: function() {
          return setDragItemIndex(null);
        },
        onDrop: function(_, dropPosition) {
          var prevIndex = dragItemIndex;
          var nextIndex = dropPosition === "left" || dropPosition === "top" ? index : index + 1;
          if (onIndexChange && prevIndex !== nextIndex) {
            onIndexChange(nextIndex, prevIndex);
          }
        },
        children: child
      });
    })
  });
}
Draggable.defaultProps = {
  direction: "vertical"
};
export {
  Draggable as default
};
//# sourceMappingURL=index.js.map
