import { useRef as C, useCallback as x, useEffect as $ } from "react";
import E from "../../../../../_virtual/interact.min.js";
import { a as H } from "../../../../sdk/dist/shared/chunk-PSCN3VVA.js";
import "../../../../sdk/dist/shared/chunk-R6ATC4TC.js";
import "@sinclair/typebox";
import "../../../../sdk/dist/shared/chunk-CFF6Q7O7.js";
import "../../../../sdk/dist/shared/chunk-IKC3FK7L.js";
import "../../../../sdk/dist/shared/chunk-E6XEI7IK.js";
import "../../../../sdk/dist/shared/chunk-SQ5T4AT5.js";
import "../../../../sdk/dist/shared/chunk-G5CJU6RD.js";
import "../../../../sdk/dist/shared/chunk-PSTFJ5IJ.js";
import "../../../../sdk/dist/shared/chunk-NJGXC7D6.js";
import "../../../../sdk/dist/shared/chunk-SXHDQ64Z.js";
import "../../../../sdk/dist/shared/chunk-QX6OGRRT.js";
import "../../../../sdk/dist/shared/chunk-FLOMNC6J.js";
import "../../../../sdk/dist/shared/chunk-PLSV746R.js";
import "../../../../sdk/dist/shared/chunk-WBZW5LVH.js";
import "../../../../sdk/dist/shared/chunk-WYVJE6J5.js";
import "../../../../sdk/dist/shared/chunk-I3VL6SRH.js";
import "../../../../sdk/dist/shared/chunk-B3EATRFX.js";
import "../../../../sdk/dist/shared/chunk-IKHZD2JH.js";
import "../../../../sdk/dist/shared/chunk-PXI7AHWS.js";
import "../../../../sdk/dist/shared/chunk-SY6DZLTC.js";
import "../../../../sdk/dist/shared/chunk-7A2JNQX3.js";
import "../../../../sdk/dist/shared/chunk-3YVJ2ELD.js";
import "../../../../sdk/dist/shared/chunk-AFVJZPLS.js";
import "../../../../sdk/dist/shared/chunk-A3GF4PYW.js";
import "../../../../sdk/dist/shared/chunk-W33JMRRN.js";
import { usePreviewMode as A } from "../../../../../use-editor.js";
function he(o, R) {
  const h = C(/* @__PURE__ */ new Set()), c = C(null), r = A(), { gridSnap: n, onResizeStart: S, onResize: k, onResizeEnd: M, enabled: b = !0 } = R, T = x((l) => {
    if (!l?.classList.contains("resizable-handle")) return "";
    const m = Array.from(l.classList).find((d) => d.startsWith("resizable-handle-"));
    return m ? m.replace("resizable-handle-", "") : "";
  }, []), w = x(
    (l, m, d, u, p) => ({
      target: l,
      rect: m,
      deltaRect: d,
      edges: u,
      activeDirection: T(p || null)
    }),
    [T]
  ), z = x(() => {
    if (!b) return;
    h.current.forEach((m) => m.unset()), h.current.clear(), document.querySelectorAll(o).forEach((m) => {
      const d = m;
      if (!d.querySelector(".resizable-handle")) return;
      const p = E(d, {
        preventDefault: "always"
      }).resizable({
        edges: {
          top: ".resizable-handle-n, .resizable-handle-ne, .resizable-handle-nw",
          right: ".resizable-handle-e, .resizable-handle-ne, .resizable-handle-se",
          bottom: ".resizable-handle-s, .resizable-handle-se, .resizable-handle-sw",
          left: ".resizable-handle-w, .resizable-handle-nw, .resizable-handle-sw"
        },
        listeners: {
          start(e) {
            const s = e.target, i = s.getBoundingClientRect(), a = e.interaction.downEvent?.target;
            s.classList.toggle("resizing", !0), L(s, i.width, i.height, e);
            const t = w(
              s,
              { width: i.width, height: i.height },
              { left: 0, top: 0, width: 0, height: 0 },
              e.edges,
              a
            );
            S?.(t);
          },
          move(e) {
            const s = e.target;
            let { width: i, height: a } = e.rect;
            const t = H[s.dataset.brickType || "unknown"] || {};
            t.minWidth?.[r] && (i = Math.max(i, t.minWidth[r])), t.minHeight?.[r] && (a = Math.max(a, t.minHeight[r])), t.maxWidth?.[r] && (i = Math.min(i, t.maxWidth[r])), t.maxHeight?.[r] && (a = Math.min(a, t.maxHeight[r])), L(s, i, a, e), e.edges.left, e.edges.right, e.edges.top, e.edges.bottom;
            const g = e.interaction.downEvent?.target, f = w(
              s,
              { width: i, height: a },
              e.deltaRect,
              e.edges,
              g
            );
            k?.(f);
          },
          end(e) {
            e.stopPropagation();
            const s = e.target, i = s.getBoundingClientRect(), a = e.interaction.downEvent?.target;
            s.classList.toggle("resizing", !1);
            const t = w(
              s,
              { width: i.width, height: i.height },
              e.deltaRect,
              e.edges,
              a
            );
            M?.(t);
          }
        },
        modifiers: [
          E.modifiers.restrictSize({
            // @ts-ignore
            min: (e, s, i) => {
              const t = i.element.dataset.brickType;
              if (!t)
                return { width: n.width, height: n.height };
              const g = H[t], f = g.minWidth?.[r] ?? n.width, y = g.minHeight?.[r] ?? n.height;
              return { width: f, height: y };
            },
            // @ts-ignore
            max: (e, s, i) => {
              const a = i.element, t = a.parentElement, g = t.clientHeight - parseFloat(getComputedStyle(t).paddingTop) - parseFloat(getComputedStyle(t).paddingBottom), f = a.dataset.brickType;
              if (!f)
                return { width: 1 / 0, height: g };
              const y = H[f], v = y.maxWidth?.[r] ?? 1 / 0, N = y.maxHeight?.[r] ?? 1 / 0;
              return { width: v, height: N };
            }
          }),
          ...n?.enabled !== !1 && n ? [
            E.modifiers.snapSize({
              endOnly: !0,
              targets: [
                E.snappers.grid({
                  width: n.width,
                  height: n.height,
                  offset: n.offset || { x: 0, y: 0 },
                  range: n.range || 1 / 0
                })
              ]
            })
          ] : []
        ].filter(Boolean)
      });
      p.on("resizeend", function() {
        window.addEventListener("click", (e) => e.stopImmediatePropagation(), {
          capture: !0,
          once: !0
        });
      }), h.current.add(p);
    });
  }, [b, o, r, n, S, k, M, w]), W = x(() => {
    c.current && c.current.disconnect(), c.current = new MutationObserver((l) => {
      l.some((d) => {
        if (d.type !== "childList") return !1;
        const u = (p) => Array.from(p).some(
          (e) => e.nodeType === Node.ELEMENT_NODE && (e.matches?.(o) || e.querySelector?.(o))
        );
        return u(d.addedNodes) || u(d.removedNodes);
      }) && setTimeout(z, 100);
    }), c.current.observe(document.body, {
      childList: !0,
      subtree: !0
    });
  }, [o, z]);
  $(() => (b ? (z(), W()) : (h.current.forEach((l) => l.unset()), h.current.clear(), c.current && c.current.disconnect()), () => {
    h.current.forEach((l) => l.unset()), h.current.clear(), c.current && c.current.disconnect();
  }), [b, z, W]);
}
function L(o, R, h, c) {
  let { x: r = 0, y: n = 0 } = o.dataset;
  r = (parseFloat(`${r}`) || 0) + c.deltaRect.left, n = (parseFloat(`${n}`) || 0) + c.deltaRect.top, requestAnimationFrame(() => {
    o.style.width = `${R}px`, o.style.height = `${h}px`, o.style.setProperty("flex-grow", "0"), o.style.transform = `translate(${r}px, ${n}px)`;
  });
}
export {
  he as useResizable
};
//# sourceMappingURL=use-resizable.js.map
