import { __rest as ee, __assign as X } from "../../../tslib@2.0.1/node_modules/tslib/tslib.es6.js";
import te from "../../../../../_virtual/index18.js";
import e from "react";
var j = function(t, n, y) {
  for (var g = t.x, b = t.y, T = (y === void 0 ? {} : y).fallbackToClosest, c = T === void 0 ? !1 : T, a = 1e4, m = -1, u = 0; u < n.length; u += 1) {
    var d = n[u];
    if (g >= d.left && g < d.right && b >= d.top && b < d.bottom)
      return u;
    if (c) {
      var l = (d.left + d.right) / 2, s = (d.top + d.bottom) / 2, h = Math.sqrt(Math.pow(g - l, 2) + Math.pow(b - s, 2));
      h < a && (a = h, m = u);
    }
  }
  return m;
}, z = function(t) {
  return {
    x: Number(t.clientX),
    y: Number(t.clientY)
  };
}, J = function(t) {
  return {
    x: Number(t.clientX),
    y: Number(t.clientY)
  };
}, Y = function(t, n) {
  return {
    x: t.x - n.x,
    y: t.y - n.y
  };
}, V = function(t) {
  t.preventDefault();
}, re = function() {
  window.addEventListener("contextmenu", V, {
    capture: !0,
    passive: !1
  });
}, Q = function() {
  window.removeEventListener("contextmenu", V);
}, ne = function(t) {
  var n = t.onStart, y = t.onMove, g = t.onEnd, b = t.allowDrag, T = b === void 0 ? !0 : b, c = t.containerRef, a = t.knobs, m = e.useRef({
    x: 0,
    y: 0
  }), u = e.useRef(void 0), d = e.useRef(!1), l = e.useRef({
    onStart: n,
    onMove: y,
    onEnd: g
  }), s = e.useState(!1), h = s[0], q = s[1];
  e.useEffect(function() {
    l.current = {
      onStart: n,
      onMove: y,
      onEnd: g
    };
  }, [n, y, g]);
  var A = function() {
    u.current && window.clearTimeout(u.current);
  }, x = e.useCallback(function() {
    if (c.current) {
      var r = c.current.getBoundingClientRect();
      m.current = {
        x: r.left,
        y: r.top
      };
    }
  }, [c]), R = e.useCallback(function(r) {
    var L = Y(r, m.current);
    l.current.onMove && l.current.onMove({
      pointInWindow: r,
      point: L
    });
  }, []), S = e.useCallback(function(r) {
    if (d.current) {
      d.current = !1;
      var L = z(r), v = Y(L, m.current);
      l.current.onStart && l.current.onStart({
        point: v,
        pointInWindow: L
      });
    } else
      R(z(r));
  }, [R]), M = e.useCallback(function(r) {
    r.cancelable ? (r.preventDefault(), R(J(r.touches[0]))) : (document.removeEventListener("touchmove", M), l.current.onEnd && l.current.onEnd());
  }, [R]), E = e.useCallback(function() {
    d.current = !1, document.removeEventListener("mousemove", S), document.removeEventListener("mouseup", E), l.current.onEnd && l.current.onEnd();
  }, [S]), P = e.useCallback(function() {
    document.removeEventListener("touchmove", M), document.removeEventListener("touchend", P), Q(), l.current.onEnd && l.current.onEnd();
  }, [M]), O = e.useCallback(function(r) {
    r.button === 0 && (a != null && a.length && !a.find(function(L) {
      return L.contains(r.target);
    }) || (document.addEventListener("mousemove", S), document.addEventListener("mouseup", E), x(), d.current = !0));
  }, [S, E, x, a]), H = e.useCallback(function(r, L) {
    document.addEventListener("touchmove", M, {
      capture: !1,
      passive: !1
    }), document.addEventListener("touchend", P), re(), l.current.onStart && l.current.onStart({
      point: r,
      pointInWindow: L
    });
  }, [P, M]), B = e.useCallback(function(r) {
    if (!(a != null && a.length && !a.find(function(i) {
      return i.contains(r.target);
    }))) {
      x();
      var L = J(r.touches[0]), v = Y(L, m.current);
      u.current = window.setTimeout(function() {
        return H(v, L);
      }, 120);
    }
  }, [H, x, a]), W = e.useCallback(function() {
    q(!0), document.removeEventListener("touchstart", W);
  }, []), N = e.useCallback(function() {
    A();
  }, []);
  return e.useLayoutEffect(function() {
    if (h) {
      var r = c.current;
      return T && (r == null || r.addEventListener("touchstart", B, {
        capture: !0,
        passive: !1
      }), document.addEventListener("touchmove", N, {
        capture: !1,
        passive: !1
      }), document.addEventListener("touchend", N, {
        capture: !1,
        passive: !1
      })), function() {
        r == null || r.removeEventListener("touchstart", B, {
          capture: !0
        }), document.removeEventListener("touchmove", N, {
          capture: !1
        }), document.removeEventListener("touchend", N, {
          capture: !1
        }), document.removeEventListener("touchmove", M), document.removeEventListener("touchend", P), Q(), A();
      };
    }
    return document.addEventListener("touchstart", W), function() {
      document.removeEventListener("touchstart", W), document.removeEventListener("mousemove", S), document.removeEventListener("mouseup", E);
    };
  }, [h, T, W, S, M, N, P, E, c, B]), h ? {} : {
    onMouseDown: O
  };
}, oe = function(t) {
  var n = e.useRef(null);
  if (!t)
    return {};
  var y = function(a) {
    n.current && (n.current.style.width = a.width + "px", n.current.style.height = a.height + "px", n.current.style.opacity = "1", n.current.style.visibility = "visible");
  }, g = function() {
    n.current && (n.current.style.opacity = "0", n.current.style.visibility = "hidden");
  }, b = function(a, m, u) {
    if (n.current) {
      var d = m[a], l = u === "y" ? d.left : m[a].left, s = u === "x" ? d.top : m[a].top;
      n.current.style.transform = "translate3d(" + l + "px, " + s + "px, 0px)";
    }
  }, T = function() {
    return /* @__PURE__ */ e.createElement("div", {
      ref: n,
      "aria-hidden": !0,
      style: {
        opacity: 0,
        visibility: "hidden",
        position: "fixed",
        top: 0,
        left: 0,
        pointerEvents: "none"
      }
    }, t);
  };
  return {
    show: y,
    hide: g,
    setPosition: b,
    render: T
  };
}, ue = "div", Z = /* @__PURE__ */ e.createContext(void 0), se = function(t) {
  var n, y = t.children, g = t.allowDrag, b = g === void 0 ? !0 : g, T = t.onSortEnd, c = t.draggedItemClassName, a = t.as, m = t.lockAxis, u = t.customHolderRef, d = t.dropTarget, l = ee(t, ["children", "allowDrag", "onSortEnd", "draggedItemClassName", "as", "lockAxis", "customHolderRef", "dropTarget"]), s = e.useRef([]), h = e.useRef([]), q = e.useRef([]), A = e.useRef(null), x = e.useRef(null), R = e.useRef(void 0), S = e.useRef(void 0), M = e.useRef({
    x: 0,
    y: 0
  }), E = oe(d);
  e.useEffect(function() {
    var v = (u == null ? void 0 : u.current) || document.body;
    return function() {
      x.current && v.removeChild(x.current);
    };
  }, [u]);
  var P = function(i) {
    if (x.current && R.current !== void 0) {
      var p = M.current, o = h.current[R.current], f = m === "y" ? o.left : i.x - p.x, C = m === "x" ? o.top : i.y - p.y;
      x.current.style.transform = "translate3d(" + f + "px, " + C + "px, 0px)";
    }
  }, O = e.useCallback(function(v) {
    if (A.current) {
      var i = s.current[v], p = h.current[v], o = i.cloneNode(!0);
      c && c.split(" ").forEach(function(w) {
        return o.classList.add(w);
      }), o.style.width = p.width + "px", o.style.height = p.height + "px", o.style.position = "fixed", o.style.margin = "0", o.style.top = "0", o.style.left = "0";
      var f = i.querySelectorAll("canvas");
      o.querySelectorAll("canvas").forEach(function(w, D) {
        var K;
        (K = w.getContext("2d")) === null || K === void 0 || K.drawImage(f[D], 0, 0);
      });
      var C = (u == null ? void 0 : u.current) || document.body;
      C.appendChild(o), x.current = o;
    }
  }, [u, c]), H = ne({
    allowDrag: b,
    containerRef: A,
    knobs: q.current,
    onStart: function(i) {
      var p, o = i.pointInWindow;
      if (A.current) {
        h.current = s.current.map(function(D) {
          return D.getBoundingClientRect();
        });
        var f = j(o, h.current);
        if (f !== -1) {
          R.current = f, O(f);
          var C = s.current[f];
          C.style.opacity = "0", C.style.visibility = "hidden";
          var w = C.getBoundingClientRect();
          M.current = {
            x: o.x - w.left,
            y: o.y - w.top
          }, P(o), (p = E.show) === null || p === void 0 || p.call(E, w), window.navigator.vibrate && window.navigator.vibrate(100);
        }
      }
    },
    onMove: function(i) {
      var p, o = i.pointInWindow;
      P(o);
      var f = R.current;
      if (!(f === void 0 || R.current === void 0)) {
        var C = h.current[R.current], w = {
          x: m === "y" ? C.left : o.x,
          y: m === "x" ? C.top : o.y
        }, D = j(w, h.current, {
          fallbackToClosest: !0
        });
        if (D !== -1) {
          S.current = D;
          for (var K = f < D, k = 0; k < s.current.length; k += 1) {
            var F = s.current[k], G = h.current[k];
            if (K && k >= f && k <= D || !K && k >= D && k <= f) {
              var U = h.current[K ? k - 1 : k + 1];
              if (U) {
                var $ = U.left - G.left, _ = U.top - G.top;
                F.style.transform = "translate3d(" + $ + "px, " + _ + "px, 0px)";
              }
            } else
              F.style.transform = "translate3d(0,0,0)";
            F.style.transitionDuration = "300ms";
          }
          (p = E.setPosition) === null || p === void 0 || p.call(E, S.current, h.current, m);
        }
      }
    },
    onEnd: function() {
      for (var i, p = 0; p < s.current.length; p += 1) {
        var o = s.current[p];
        o.style.transform = "", o.style.transitionDuration = "";
      }
      var f = R.current;
      if (f !== void 0) {
        var C = s.current[f];
        C && (C.style.opacity = "1", C.style.visibility = "");
        var w = S.current;
        w !== void 0 && f !== w && (s.current = te(s.current, f, w), T(f, w));
      }
      if (R.current = void 0, S.current = void 0, (i = E.hide) === null || i === void 0 || i.call(E), x.current) {
        var D = (u == null ? void 0 : u.current) || document.body;
        D.removeChild(x.current), x.current = null;
      }
    }
  }), B = e.useCallback(function(v) {
    s.current.push(v);
  }, []), W = e.useCallback(function(v) {
    var i = s.current.indexOf(v);
    i !== -1 && s.current.splice(i, 1);
  }, []), N = e.useCallback(function(v) {
    q.current.push(v);
  }, []), r = e.useCallback(function(v) {
    var i = q.current.indexOf(v);
    i !== -1 && q.current.splice(i, 1);
  }, []), L = e.useMemo(function() {
    return {
      registerItem: B,
      removeItem: W,
      registerKnob: N,
      removeKnob: r
    };
  }, [B, W, N, r]);
  return /* @__PURE__ */ e.createElement(a || ue, X(X(X({}, b ? H : {}), l), {
    ref: A
  }), /* @__PURE__ */ e.createElement(Z.Provider, {
    value: L
  }, y, (n = E.render) === null || n === void 0 ? void 0 : n.call(E)));
}, ve = function(t) {
  var n = t.children, y = e.useContext(Z);
  if (!y)
    throw new Error("SortableItem must be a child of SortableList");
  var g = y.registerItem, b = y.removeItem, T = e.useRef(null);
  return e.useEffect(function() {
    var c = T.current;
    return c && g(c), function() {
      c && b(c);
    };
  }, [g, b, n]), /* @__PURE__ */ e.cloneElement(n, {
    ref: T
  });
};
export {
  ve as SortableItem,
  se as default
};
//# sourceMappingURL=index.module.js.map
