import { useEffect as E, useCallback as H } from "react";
const B = (I) => {
  const { onResizeStart: x, onResize: R, onResizeStop: S, stateRef: t, setCurrentState: d, containerElement: n, allowHeightResizeContainer: w, allowWidthResizeContainer: y } = I;
  E(() => {
    if (!n)
      return;
    const h = (c) => {
      const s = c.target.scrollTop, r = c.target.scrollLeft;
      t.current.resizing && console.warn("handleScroll IMPLEMENTAR", s, r);
    };
    return n.addEventListener("scroll", h), () => {
      n?.removeEventListener("scroll", h);
    };
  }, [n, d, t]);
  const L = H(
    (h, c, s) => {
      x && x(h, c, s);
      const r = t.current.width, o = t.current.height, i = {
        x: t.current.x,
        y: t.current.y,
        width: r,
        height: o,
        handle: c,
        widthInitial: r,
        heightInitial: o
      }, l = {
        x: t.current.x,
        width: t.current.width,
        y: t.current.y,
        height: t.current.height
      };
      d({
        ...l,
        resizing: i
      }), t.current = {
        ...t.current,
        ...l,
        resizing: i
      };
    },
    [x, d, t]
  ), T = H(
    (h, c, s, r, o) => {
      if (!t.current.resizing || !n)
        return;
      R && R(h, c, s, r, o);
      const i = (t.current.resizing?.widthInitial || 0) + r.width, l = (t.current.resizing?.heightInitial || 0) + r.height, z = Math.round(o.x), e = Math.round(o.y), u = {
        x: z,
        width: i,
        y: e,
        height: l
      };
      let a, g;
      const f = t.current.containerSize?.containerWidth || n.clientWidth, b = t.current.containerSize?.containerHeight || n.clientHeight, m = u.x + u.width;
      if (m > f && !y && c.includes("right")) {
        const v = n.scrollLeft + f;
        m > v && (a = m - f + 100);
      }
      const p = u.y + u.height;
      if (p > b && w && c.includes("bottom")) {
        const v = n.scrollTop + b;
        p > v && (g = p - b + 100);
      }
      (a !== void 0 || g !== void 0) && setTimeout(() => {
        n.scrollTo({
          left: a,
          top: g,
          behavior: "instant"
        });
      }, 10);
    },
    [w, y, n, R, t]
  ), W = H(
    (h, c, s, r, o) => {
      S && S(h, c, s, r, o);
      const i = {
        left: t.current.bounds?.left || 0,
        top: t.current.bounds?.top || 0,
        right: t.current.bounds?.right || 0,
        bottom: t.current.bounds?.bottom || 0
      }, l = (t.current.resizing?.widthInitial || 0) + r.width, z = (t.current.resizing?.heightInitial || 0) + r.height, e = {
        x: o.x,
        width: l,
        y: o.y,
        height: z
      };
      if (!n)
        return;
      const u = t.current.containerSize?.containerWidth || n.clientWidth, a = t.current.containerSize?.containerHeight || n.clientHeight;
      e.x < i.left ? (e.x = i.left, e.width = l - (i.left - o.x)) : e.x + e.width > u + i.right && (y || (e.width = u - e.x + i.right)), e.y < i.top ? (e.y = i.top, e.height = z - (i.top - o.y)) : e.y + e.height > a + i.bottom && (w || (e.height = a - e.y + i.bottom));
      const g = Math.round(e.x), f = Math.round(e.y);
      e.x = g, e.y = f, d({
        ...e,
        resizing: void 0
      }), t.current = {
        ...t.current,
        ...e,
        resizing: void 0
      };
    },
    [w, y, n, S, d, t]
  );
  return { localOnResize: T, localOnResizeStart: L, localOnResizeStop: W };
};
export {
  B as useResizeOptions
};
