"use strict";

exports.__esModule = true;
exports["default"] = exports.BaseSortable = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _core = require("@dnd-kit/core");
var _sortable = require("@dnd-kit/sortable");
var _throttleDebounce = require("throttle-debounce");
var _flex = _interopRequireDefault(require("../../../components/templates/flex"));
var _useNavigationScroll = _interopRequireDefault(require("../hooks/useNavigationScroll"));
var _useNavigationArrows2 = _interopRequireDefault(require("../hooks/useNavigationArrows"));
var _useOnTabsResize = _interopRequireDefault(require("../hooks/useOnTabsResize"));
var _utils = require("../../../utils");
var _item = _interopRequireDefault(require("./item"));
var _container = _interopRequireDefault(require("./container"));
var _arrow = _interopRequireDefault(require("./arrow"));
var _excluded = ["activationConstraint", "collisionDetection", "measuring", "modifiers"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var dropAnimationConfig = {
  sideEffects: (0, _core.defaultDropAnimationSideEffects)({
    styles: {
      active: {
        opacity: "0.5"
      }
    }
  })
};
var BaseSortable = exports.BaseSortable = function BaseSortable(_ref) {
  var _ref$animateLayoutCha = _ref.animateLayoutChanges,
    animateLayoutChanges = _ref$animateLayoutCha === void 0 ? function (args) {
      return (0, _sortable.defaultAnimateLayoutChanges)(_extends({}, args, {
        wasDragging: true
      }));
    } : _ref$animateLayoutCha,
    _ref$adjustScale = _ref.adjustScale,
    adjustScale = _ref$adjustScale === void 0 ? false : _ref$adjustScale,
    _ref$Container = _ref.Container,
    Container = _ref$Container === void 0 ? _container["default"] : _ref$Container,
    _ref$dropAnimation = _ref.dropAnimation,
    dropAnimation = _ref$dropAnimation === void 0 ? dropAnimationConfig : _ref$dropAnimation,
    _ref$strategy = _ref.strategy,
    strategy = _ref$strategy === void 0 ? _sortable.horizontalListSortingStrategy : _ref$strategy,
    _onDragEnd = _ref.onDragEnd,
    onTabClose = _ref.onTabClose,
    onResize = _ref.onResize,
    items = _ref.items,
    Item = _ref.Item,
    parentRef = _ref.parentRef,
    collapsed = _ref.collapsed,
    ref = _ref.ref;
  var _useState = (0, _react.useState)(null),
    activeId = _useState[0],
    setActiveId = _useState[1];
  var getIndex = function getIndex(id) {
    return items.findIndex(function (item) {
      return item.id === id;
    });
  };
  var activeIndex = activeId !== null ? getIndex(activeId) : -1;
  var containerRef = (0, _react.useRef)();
  (0, _useNavigationScroll["default"])(containerRef);
  var lastTabRef = (0, _react.useRef)();
  var onTabsResize = (0, _useOnTabsResize["default"])(parentRef, containerRef, lastTabRef, onResize, [items.length]);
  var _useNavigationArrows = (0, _useNavigationArrows2["default"])(containerRef, lastTabRef, items.length, collapsed),
    arrowLeft = _useNavigationArrows[0],
    arrowRight = _useNavigationArrows[1],
    onScroll = _useNavigationArrows[2];
  (0, _react.useEffect)(function () {
    if (!containerRef.current) return;
    onTabsResize();
    var container = containerRef.current;
    var handlers = (0, _throttleDebounce.debounce)(300, function () {
      onTabsResize();
      onScroll();
    });
    handlers();
    container.addEventListener("scroll", onScroll);
    window.addEventListener("resize", handlers);
    return function () {
      container.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", handlers);
    };
  }, [items.length, collapsed]);
  var scrollLeft = function scrollLeft(e) {
    e.preventDefault();
    containerRef.current.scrollTo({
      left: containerRef.current.scrollLeft - 100,
      behavior: "smooth"
    });
  };
  var scrollRight = function scrollRight(e) {
    e.preventDefault();
    containerRef.current.scrollTo({
      left: containerRef.current.scrollLeft + 100,
      behavior: "smooth"
    });
  };
  (0, _core.useDndMonitor)({
    onDragStart: function onDragStart(_ref2) {
      var active = _ref2.active;
      if (!active || !active.data.current.navigationTab) return;
      setActiveId(active.id);
    },
    onDragEnd: function onDragEnd(_ref3) {
      var active = _ref3.active,
        over = _ref3.over;
      if (!active || !active.data.current.navigationTab) return;
      setActiveId(null);
      if (over && over.data.current.navigationTab) {
        var overIndex = getIndex(over.id);
        if (activeIndex !== overIndex) {
          _onDragEnd(activeIndex, overIndex);
        }
      }
    },
    onDragCancel: function onDragCancel() {
      return setActiveId(null);
    }
  });
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    overflow: "hidden",
    position: "relative"
  }, arrowLeft && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
    onClick: scrollLeft,
    name: "navLeft"
  }), /*#__PURE__*/_react["default"].createElement(Container, {
    ref: (0, _utils.mergeRefs)(containerRef, ref)
  }, /*#__PURE__*/_react["default"].createElement(_sortable.SortableContext, {
    items: items,
    strategy: strategy
  }, items.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(_item["default"], _extends({
      key: item.id + "-" + index,
      itemProps: item,
      id: item.id,
      draggable: !item.fixed,
      index: index,
      onRemove: onTabClose,
      animateLayoutChanges: animateLayoutChanges,
      Item: Item
    }, index === items.length - 1 && {
      lastTabRef: lastTabRef
    }, {
      collapsed: collapsed
    }));
  }))), arrowRight && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
    onClick: scrollRight,
    name: "navRight",
    right: true
  })), /*#__PURE__*/(0, _reactDom.createPortal)(/*#__PURE__*/_react["default"].createElement(_core.DragOverlay, {
    adjustScale: adjustScale,
    dropAnimation: dropAnimation
  }, activeId !== null ? /*#__PURE__*/_react["default"].createElement(Item, _extends({}, items[activeIndex], {
    backgroundOpacity: 0.8,
    isDragOverlay: true,
    grabbing: true,
    sorting: true
  })) : null), document.body));
};
var Sortable = function Sortable(_ref4) {
  var activationConstraint = _ref4.activationConstraint,
    _ref4$collisionDetect = _ref4.collisionDetection,
    collisionDetection = _ref4$collisionDetect === void 0 ? _core.closestCenter : _ref4$collisionDetect,
    _ref4$measuring = _ref4.measuring,
    measuring = _ref4$measuring === void 0 ? {
      droppable: {
        strategy: _core.MeasuringStrategy.Always
      }
    } : _ref4$measuring,
    modifiers = _ref4.modifiers,
    rest = _objectWithoutPropertiesLoose(_ref4, _excluded);
  var sensors = (0, _core.useSensors)((0, _core.useSensor)(_core.MouseSensor, {
    activationConstraint: activationConstraint
  }), (0, _core.useSensor)(_core.TouchSensor, {
    activationConstraint: activationConstraint
  }));
  return /*#__PURE__*/_react["default"].createElement(_core.DndContext, {
    sensors: sensors,
    collisionDetection: collisionDetection,
    measuring: measuring,
    modifiers: modifiers
  }, /*#__PURE__*/_react["default"].createElement(BaseSortable, rest));
};
var _default = exports["default"] = Sortable;