import { jsx as N } from "react/jsx-runtime";
import { useRef as m, useEffect as X } from "react";
import { noop as h } from "es-toolkit/function";
import Y from "../../utils/classNames.js";
const z = "x", D = "y", M = "left", O = "right", P = "top", S = "bottom", w = (i, s, n) => {
  if (!i)
    return;
  const t = i.ownerDocument;
  return t.addEventListener("mouseup", s), t.addEventListener("mousemove", n), t.addEventListener("touchend", s), t.addEventListener("touchmove", n), {
    remove: () => {
      t.removeEventListener("mouseup", s), t.removeEventListener("mousemove", n), t.removeEventListener("touchend", s), t.removeEventListener("touchmove", n);
    }
  };
}, o = (i) => {
  const {
    className: s = "",
    direction: n = z,
    position: t = O,
    onResizeStart: E = h,
    onResize: d = h,
    onResizeEnd: x = h,
    children: y,
    ...I
  } = i, u = m(null), l = m(null), f = m(!1), c = m({ x: 0, y: 0 });
  X(
    () => () => {
      R();
    },
    []
  );
  const v = (e) => {
    if (window.TouchEvent && e instanceof window.TouchEvent)
      return e.touches[0].clientX;
    if (e instanceof MouseEvent)
      return e.clientX;
    throw new Error("Unsupported event type");
  }, T = (e) => {
    if (window.TouchEvent && e instanceof window.TouchEvent)
      return e.touches[0].clientY;
    if (e instanceof MouseEvent)
      return e.clientY;
    throw new Error("Unsupported event type");
  }, g = (e) => {
    f.current = !0;
    const r = e.nativeEvent, { clientX: a, clientY: H } = r;
    c.current = {
      x: a,
      y: H
    }, E(e), u.current = w(l.current, L, p);
  }, A = (e) => {
    f.current = !0;
    const a = e.nativeEvent.touches[0];
    c.current = {
      x: a.clientX,
      y: a.clientY
    }, E(e), u.current = w(l.current, L, p);
  }, p = (e) => {
    if (f.current) {
      if (n === o.HORIZONTAL) {
        const r = c.current.x - v(e);
        r !== 0 && d(r);
      }
      if (n === o.VERTICAL) {
        const r = c.current.y - T(e);
        r !== 0 && d(r);
      }
      c.current = {
        x: v(e),
        y: T(e)
      };
    }
  }, L = (e) => {
    f.current = !1, x(e), R();
  }, R = () => {
    u.current && u.current.remove();
  }, C = Y(
    "Resizer",
    n === "x" && "resize-horizontal",
    n === "y" && "resize-vertical",
    t === "left" && "resize-left",
    t === "right" && "resize-right",
    t === "top" && "resize-top",
    t === "bottom" && "resize-bottom",
    s && s
  );
  return /* @__PURE__ */ N(
    "div",
    {
      ref: l,
      className: C,
      onMouseDown: g,
      onTouchStart: A,
      ...I,
      children: y
    }
  );
};
o.HORIZONTAL = z;
o.VERTICAL = D;
o.LEFT = M;
o.RIGHT = O;
o.TOP = P;
o.BOTTOM = S;
export {
  o as default
};
//# sourceMappingURL=Resizer.js.map
