import { useCallback as x } from "react";
const k = (M) => {
  const {
    containerElement: e,
    onDragStart: S,
    onDrag: L,
    onDragStop: W,
    transformScale: u,
    stateRef: t,
    setCurrentState: H,
    allowHeightResizeContainer: p,
    allowWidthResizeContainer: m
  } = M, D = x(
    (g, a) => {
      const { node: r } = a, n = { y: 0, x: 0, width: 0, height: 0 };
      if (!e)
        return;
      if (g.stopPropagation(), S && S(g, a) === !1)
        return !1;
      const f = e.getBoundingClientRect(), s = r.getBoundingClientRect(), h = s.left / u, c = f.left / u, i = s.top / u, o = f.top / u;
      n.x = h - c + e.scrollLeft, n.y = i - o + e.scrollTop, n.width = t.current.width, n.height = t.current.height, t.current.dragging = n, H((l) => ({
        ...l,
        ...n,
        shrinkLeftAt: void 0,
        //Cancelar los flags de memoria de encogimiento del contenedor padre
        shrinkTopAt: void 0,
        //Cancelar los flags de memoria de encogimiento del contenedor padre
        overflowWidthAt: void 0,
        //Cancelar los flags de memoria de encogimiento del contenedor padre
        overflowHeightAt: void 0,
        //Cancelar los flags de memoria de encogimiento del contenedor padre
        dragging: n
      }));
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [e, S, u]
  ), z = x((g, a) => {
    const { x: r, y: n, deltaX: f, deltaY: s } = a;
    if (g.stopPropagation(), !t.current.dragging || !e)
      return;
    if (L && L(g, a) === !1)
      return !1;
    const h = t.current.height, c = t.current.width, i = n + h, o = e.scrollTop, l = e.clientHeight, d = e.clientWidth, b = e.scrollLeft, y = o, B = y + l, C = e.scrollLeft, O = C + e.clientWidth, v = 15;
    let T, w;
    if (s < 0 && n < y && o > 0) {
      const R = Math.min(15, Math.min(v, o));
      w = e.scrollTop - R;
    } else s > 0 && p && i > B && (h > l ? w = n : w = n + v);
    if (f < 0 && r < C && b > 0) {
      const R = Math.min(15, Math.min(v, b));
      T = e.scrollLeft - R;
    } else f > 0 && m && r > O && (c > d ? T = r : T = r + v);
    (T !== void 0 || w !== void 0) && setTimeout(() => {
      e.scrollTo({
        left: T,
        top: w,
        behavior: "smooth"
      });
    }, 10), t.current.dragging = {
      ...t.current.dragging,
      x: r,
      y: n
    };
  }, [p, m, e, L, t]), A = x(
    (g, a) => {
      if (!t.current.dragging || !e)
        return;
      const r = t.current.containerSize?.containerWidth || e.clientWidth, n = t.current.containerSize?.containerHeight || e.clientHeight, f = t.current.dragging.width, s = t.current.dragging.height, h = 15, c = {
        left: t.current.bounds?.left || 0,
        top: t.current.bounds?.top || 0,
        right: t.current.bounds?.right || 0,
        bottom: t.current.bounds?.bottom || 0
      };
      let i = t.current.dragging.x, o = t.current.dragging.y;
      i < c.left ? i = c.left : i + t.current.width > r + c.right && (m || (i = r - f + c.right)), o < c.top ? o = c.top : o + s > n + c.bottom && (p || (o = n - s + c.bottom));
      let l, d;
      m ? i > r && (f < r ? l = i + h : l = i) : e.scrollLeft > 0 && (l = 0), p ? o > n && (s < n ? d = o + h : d = o) : e.scrollTop > 0 && (d = 0), (l !== void 0 || d !== void 0) && setTimeout(() => {
        e.scrollTo({
          left: l,
          top: d,
          behavior: "smooth"
        });
      }, 10), i = Math.round(i), o = Math.round(o), t.current = {
        ...t.current,
        x: i,
        y: o,
        dragging: void 0
      }, H((b) => ({
        ...b,
        x: i,
        y: o,
        dragging: void 0
      })), W && W(g, a);
    },
    [t, e, H, W, m, p]
  );
  return { localOnDragStart: D, localOnDrag: z, localOnDragStop: A };
};
export {
  k as useDragOptions
};
