/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { onUnmounted } from 'vue';

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var traversalTabNavs = function traversalTabNavs(tabNavs, fn) {
  var _iterator = _createForOfIteratorHelper(tabNavs),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var itemNode = _step.value;
      if (itemNode.getAttribute("draggable")) fn(itemNode);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
};
var handleTarget = function handleTarget(target, tabNavs) {
  var resultTarget;
  traversalTabNavs(tabNavs, function (itemNode) {
    if (itemNode.contains(target)) resultTarget = itemNode;
  });
  return resultTarget;
};
function useDragSort(props) {
  var navsWrap = null;
  var dragged;
  var enterTargets = [];
  var dragstart = function dragstart(event) {
    var target = event.target;
    dragged = target;
    target.style.opacity = "0.5";
  };
  var dragend = function dragend(event) {
    event.target.style.opacity = "";
  };
  var dragover = function dragover(event) {
    event.preventDefault();
  };
  var dragenter = function dragenter(event) {
    var target = handleTarget(event.target, navsWrap.children);
    if (target && target !== dragged && target.draggable) {
      target.firstChild.style.outline = "1px dashed #0052d9";
      if (!enterTargets.includes(target)) enterTargets.push(target);
    }
  };
  var dragleave = function dragleave(event) {
    var target = event.target;
    for (var _i = 0, _enterTargets = enterTargets; _i < _enterTargets.length; _i++) {
      var enterTarget = _enterTargets[_i];
      if (!enterTarget.contains(target)) {
        enterTarget.firstChild.style.outline = "none";
      }
    }
  };
  var drop = function drop(event) {
    event.preventDefault();
    traversalTabNavs(navsWrap.children, function (tabNav) {
      tabNav.firstChild.style.outline = "none";
    });
    var target = handleTarget(event.target, navsWrap.children);
    if (target && target.parentNode !== dragged && target.draggable) {
      var dragIndex = [].indexOf.call(navsWrap.children, dragged);
      var targetIndex = [].indexOf.call(navsWrap.children, target);
      if (targetIndex > dragIndex) target = navsWrap.children[targetIndex + 1];
      navsWrap.insertBefore(dragged, target);
      var currentIndex = props.theme === "card" ? dragIndex : dragIndex - 1;
      var endIndex = props.theme === "card" ? targetIndex : targetIndex - 1;
      props.onDragSort({
        currentIndex: currentIndex,
        current: props.panels[currentIndex].value,
        targetIndex: endIndex,
        target: props.panels[endIndex].value
      });
    }
  };
  function setNavsWrap(val) {
    navsWrap = val;
    navsWrap.addEventListener("dragstart", dragstart, false);
    navsWrap.addEventListener("dragend", dragend, false);
    navsWrap.addEventListener("dragover", dragover, false);
    navsWrap.addEventListener("dragenter", dragenter, false);
    document.addEventListener("dragleave", dragleave, false);
    document.addEventListener("mousemove", dragleave, false);
    navsWrap.addEventListener("drop", drop, false);
  }
  onUnmounted(function () {
    navsWrap.removeEventListener("dragstart", dragstart);
    navsWrap.removeEventListener("dragend", dragend);
    navsWrap.removeEventListener("dragover", dragover);
    navsWrap.removeEventListener("dragenter", dragenter);
    document.removeEventListener("dragleave", dragleave);
    document.removeEventListener("mousemove", dragleave);
    navsWrap.removeEventListener("drop", drop);
  });
  return {
    setNavsWrap: setNavsWrap
  };
}

export { useDragSort as default };
//# sourceMappingURL=useDragSort.js.map
