function L(e, t) {
  const a = Element.prototype;
  return (a.matchesSelector || a.mozMatchesSelector || a.msMatchesSelector || a.oMatchesSelector || a.webkitMatchesSelector || function(l) {
    const h = this, v = (h.document || h.ownerDocument).querySelectorAll(l);
    let d = v.length;
    for (; --d >= 0 && v.item(d) !== h; )
      ;
    return d > -1;
  }).call(e, t);
}
const S = {
  mounted(e, t) {
    let a, c, l, h, v;
    const d = () => {
      var r, s;
      if ((r = t == null ? void 0 : t.value) != null && r.handle) {
        const o = (s = t == null ? void 0 : t.value) == null ? void 0 : s.handle;
        let u = h;
        for (; u !== e; ) {
          if (L(u, o))
            return !0;
          u = u.parentNode;
        }
        return !1;
      }
      return !0;
    }, E = (r) => {
      let s = -1, o = r.target;
      for (; o !== e; )
        o.parentNode === e && (s = Array.from(e.children).indexOf(o)), o = o.parentNode;
      return s;
    }, p = () => {
      var r;
      return ((r = t == null ? void 0 : t.value) == null ? void 0 : r.dragClass) || "gui-drag-item";
    }, m = (r, s) => {
      t != null && t.value[r] && (t == null || t.value[r](s));
    };
    e.addEventListener("mouseover", (r) => {
      h = r.target;
    }), Array.from(e.children).forEach((r) => {
      r.setAttribute("draggable", "true"), r.addEventListener("dragstart", (s) => {
        d() ? (v = !1, a = r, c = Array.from(e.children).indexOf(a), setTimeout(() => {
          a.classList.add(p());
        })) : (s.preventDefault(), s.stopPropagation()), m("dragStart", s);
      });
    }), e.addEventListener("dragenter", function(r) {
      m("dragEnter", r);
    }), e.addEventListener("dragover", function(r) {
      var u, f;
      const s = Array.from(e.children).indexOf(a), o = E(r);
      s !== -1 && o !== -1 && (s > o ? (e.removeChild(a), e.insertBefore(a, e.children[o]), l = o) : s < o && (e.removeChild(a), e.children[o] ? (e.insertBefore(a, e.children[o]), l = o) : (e.appendChild(a), l = e.children.length - 1)), (u = t == null ? void 0 : t.value) != null && u.dragover && ((f = t == null ? void 0 : t.value) == null || f.dragover(r))), r.preventDefault(), m("dragOver", r);
    }), e.addEventListener("dragleave", function(r) {
      m("dragLeave", r);
    }), e.addEventListener("drop", function(r) {
      var s, o, u;
      if (v = !0, (s = t == null ? void 0 : t.value) != null && s.list) {
        const f = (o = t == null ? void 0 : t.value) == null ? void 0 : o.list, x = f[c];
        f.splice(c, 1), f.splice(l, 0, x), (u = t == null ? void 0 : t.value) != null && u.drop && m("drop", {
          event: r,
          list: f,
          fromIndex: c,
          targetIndex: l
        });
      }
    }), e.addEventListener("dragend", function(r) {
      v || c !== -1 && l !== -1 && (l > c ? (e.removeChild(a), e.insertBefore(a, e.children[c])) : c > l && (e.removeChild(a), e.children[c] ? e.insertBefore(a, e.children[c]) : e.appendChild(a))), c = -1, l = -1, setTimeout(() => {
        a.classList.remove(p());
      }), m("dragEnd", r);
    });
  }
}, C = {
  title: "DragDrop 拖拽",
  category: "高级组件",
  status: "100%",
  install(e) {
    e.directive("GSortable", S);
  }
};
export {
  S as SortableDirective,
  C as default
};
