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