function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 ? 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
    }
  });
}