import { jsx as de } from "@emotion/react/jsx-runtime";
import { forwardRef as he, useState as me, useMemo as v, useRef as C, useImperativeHandle as pe } from "react";
import ze from "clsx";
import { dragResizeWindowRNDClasses as o } from "./classes/index.js";
import { DEFAULT_TRANSFORM_SCALE as fe } from "./constants.js";
import { useDragOptions as Re } from "./hooks/useRNDDragOptions.js";
import { useResizeOptions as ue } from "./hooks/useRNDReResizeOptions.js";
import { useRNDDimensionEffects as xe } from "./hooks/useRNDDimensionEffects.js";
import { RndStyled as we } from "./slots/DragResizeWindowRNDSlots.js";
import { getInitialSize as Se } from "./helpers/getInitialSize.js";
const Be = he((I, y) => {
  const {
    //Comunes
    style: L,
    className: O,
    children: D,
    defaultPosition: N = { vertical: { top: 0, height: 100 }, horizontal: { left: 0, width: 100 } },
    containerElement: t,
    containerSize: i,
    transformScale: T = fe,
    bounds: f = { left: 5, top: 5, right: -5, bottom: -5 },
    minWidth: B = 200,
    minHeight: V = 100,
    minWindowWidth: A,
    minWindowHeight: E,
    resizeHandles: be,
    // maxWidth = 1000,
    // maxHeight = 1000,
    //Draggable
    draggableHandle: M = `${o.draggableHandle}`,
    draggableCancel: F = `.${o.draggableCancel}`,
    draggable: P = !0,
    onDragStart: _,
    onDrag: U,
    onDragStop: $,
    onMouseDown: j,
    //resizable
    resizable: k = !0,
    onResizeStart: q,
    onResize: G,
    onResizeStop: J,
    hidden: W,
    allowHeightResizeContainer: l = !1,
    allowWidthResizeContainer: s = !1,
    windowRef: He,
    //Otras
    ...K
  } = I, g = A ?? B, c = E ?? V, [e, R] = me(
    () => Se(i, f, N, t, l, s)
  ), Q = v(() => [g, c], [g, c]), n = C({
    bounds: f,
    data: void 0,
    dragging: void 0,
    containerSize: i ? { ...i } : void 0,
    x: e.x,
    y: e.y,
    width: e.width,
    height: e.height,
    minConstraints: Q,
    magnetizeRight: e.magnetizeRight,
    magnetizeBottom: e.magnetizeBottom
  });
  i && (n.current.containerSize?.containerWidth !== i.containerWidth || n.current.containerSize?.containerHeight !== i.containerHeight) && (n.current.containerSize = { ...i });
  const r = C(null);
  pe(y, () => ({
    /**
     * "ensureVisible" enfoca la ventana para que esté visible, cuando se requiere desde afuera ubicarla en el contenedor padre
     */
    ensureVisible: () => {
      if (!r.current || !t)
        return;
      const a = t.getBoundingClientRect(), h = r.current.getBoundingClientRect(), u = h.height, x = h.width, m = h.top - a.top + t.scrollTop, p = h.left - a.left + t.scrollLeft, ae = m + u, le = p + x, w = t.clientHeight, S = t.clientWidth, se = t.scrollTop + w, z = 0.5, ge = m >= t.scrollTop - z && ae <= se + z, ce = p >= t.scrollLeft - z && le <= t.scrollLeft + S + z;
      if (!ge || !ce) {
        let b, H;
        u > w ? b = m : b = m - w / 2 + u / 2, x > S ? H = p : H = p - S / 2 + x / 2, t.scrollTo({
          top: Math.max(0, b),
          left: Math.max(0, H),
          behavior: "smooth"
        });
      }
    },
    /**
     * "current" devuelve el elemento DOM de la ventana
     */
    current: r.current
  }), [t]);
  const { localOnDragStart: X, localOnDrag: Y, localOnDragStop: Z } = Re({
    allowHeightResizeContainer: l,
    allowWidthResizeContainer: s,
    containerElement: t,
    transformScale: T,
    onDragStart: _,
    onDrag: U,
    onDragStop: $,
    stateRef: n,
    setCurrentState: R
  }), { localOnResize: ee, localOnResizeStart: te, localOnResizeStop: ie } = ue({
    allowHeightResizeContainer: l,
    allowWidthResizeContainer: s,
    containerElement: t,
    onResizeStart: q,
    onResize: G,
    onResizeStop: J,
    stateRef: n,
    setCurrentState: R
  });
  xe({
    containerElement: t,
    bounds: f,
    containerSize: i,
    setCurrentState: R,
    stateRef: n,
    minWindowWidth: g,
    minWindowHeight: c,
    wrapperRef: r,
    allowHeightResizeContainer: l,
    allowWidthResizeContainer: s
  });
  const ne = {
    hidden: W,
    dragging: !!e.dragging,
    resizing: !!e.resizing,
    draggingActive: !!e.dragging,
    x: e.x,
    y: e.y,
    width: e.width,
    height: e.height
  }, oe = ze(O, {
    //Requerido para el componente Draggable
    [o.hidden]: W,
    [o.dragging]: !!e.dragging,
    [o.resizing]: !!e.resizing,
    [o.draggingActive]: !!e.dragging
    // dropping: Boolean(droppingPosition),
  }), re = v(() => D, [D]);
  if (!t)
    return null;
  const d = "5px";
  return /* @__PURE__ */ de(
    we,
    {
      ownerState: ne,
      ...K,
      onMouseDown: j,
      ref: (a) => {
        a && (r.current = a.resizableElement.current);
      },
      style: { ...L },
      dragHandleClassName: M,
      enableResizing: k,
      disableDragging: !P,
      minWidth: g,
      minHeight: c,
      cancel: F,
      className: oe,
      enableUserSelectHack: !0,
      resizeHandleStyles: {
        zIndex: 1,
        //Se debe mover lo handler de resize para queden dentro de la ventana, o genería un scroll innecesario.
        bottom: { bottom: "0px", zIndex: 1, height: d },
        right: { right: "0px", zIndex: 1, width: d },
        top: { top: "0px", zIndex: 1, height: d },
        left: { left: "0px", zIndex: 1, width: d },
        bottomRight: { bottom: "0px", right: "0px", zIndex: 1 },
        bottomLeft: { bottom: "0px", left: "0px", zIndex: 1 },
        topRight: { top: "0px", right: "0px", zIndex: 1 },
        topLeft: { top: "0px", left: "0px", zIndex: 1 }
      },
      resizeHandleClasses: {
        left: "resize-handle-left",
        right: "resize-handle-right",
        top: "resize-handle-top",
        bottom: "resize-handle-bottom",
        bottomRight: "resize-handle-bottom-right",
        bottomLeft: "resize-handle-bottom-left",
        topRight: "resize-handle-top-right",
        topLeft: "resize-handle-top-left"
      },
      size: { width: e.width, height: e.height },
      position: { x: e.x, y: e.y },
      onDragStart: X,
      onDrag: Y,
      onDragStop: Z,
      onResizeStart: te,
      onResize: ee,
      onResizeStop: ie,
      children: re
    }
  );
});
export {
  Be as DragResizeWindowRND,
  Be as default
};
