function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { DndContext, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
import { SortableContext, arrayMove, useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { isFunction } from "@pisell/utils";
import React, { useCallback, useMemo } from "react";
export default function Sortable(_ref) {
  var _ref$list = _ref.list,
    list = _ref$list === void 0 ? [] : _ref$list,
    onChange = _ref.onChange,
    rowKey = _ref.rowKey,
    children = _ref.children,
    activationConstraint = _ref.activationConstraint;
  var sensors = useSensors(useSensor(PointerSensor, {
    activationConstraint: activationConstraint
  }));
  var getRowKey = useCallback(function (row) {
    if (isFunction(rowKey)) {
      return rowKey(row);
    }
    return row[rowKey];
  }, [rowKey]);
  var items = useMemo(function () {
    return list === null || list === void 0 ? void 0 : list.map(function (item) {
      return getRowKey(item);
    });
  }, [list, rowKey]);
  return /*#__PURE__*/React.createElement(DndContext, {
    onDragEnd: function onDragEnd(_ref2) {
      var over = _ref2.over,
        active = _ref2.active;
      if (active.id !== (over === null || over === void 0 ? void 0 : over.id)) {
        var activeIndex = list === null || list === void 0 ? void 0 : list.findIndex(function (i) {
          return getRowKey(i) === active.id;
        });
        var overIndex = list === null || list === void 0 ? void 0 : list.findIndex(function (i) {
          return getRowKey(i) === (over === null || over === void 0 ? void 0 : over.id);
        });
        onChange === null || onChange === void 0 || onChange(arrayMove(list, activeIndex, overIndex));
      }
    },
    sensors: sensors
  }, /*#__PURE__*/React.createElement(SortableContext, {
    items: items
  }, list.map(function (item, index) {
    return /*#__PURE__*/React.createElement(SortableItem, {
      key: getRowKey(item),
      id: getRowKey(item),
      item: item,
      index: index,
      disabled: !!(item !== null && item !== void 0 && item.disabled)
    }, children);
  })));
}
export function SortableItem(_ref3) {
  var id = _ref3.id,
    index = _ref3.index,
    item = _ref3.item,
    children = _ref3.children,
    disabled = _ref3.disabled;
  var _useSortable = useSortable({
      id: id,
      disabled: disabled
    }),
    attributes = _useSortable.attributes,
    isSorting = _useSortable.isSorting,
    listeners = _useSortable.listeners,
    setNodeRef = _useSortable.setNodeRef,
    setActivatorNodeRef = _useSortable.setActivatorNodeRef,
    transform = _useSortable.transform,
    transition = _useSortable.transition;
  return children({
    item: item,
    index: index,
    style: {
      transform: CSS.Transform.toString(transform && _objectSpread(_objectSpread({}, transform), {}, {
        scaleY: 1
      })),
      transition: transition
    },
    setActivatorNodeRef: setActivatorNodeRef,
    listeners: listeners,
    attributes: attributes,
    props: {
      ref: setNodeRef
    }
  });
}