'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const utils = require('../../../../lib/utils.cjs');
const LucideIcons = require('lucide-react');
const useGestureReact_esm = require('../../../../../../../node_modules/.pnpm/@use-gesture_react@10.3.1_react@19.2.0/node_modules/@use-gesture/react/dist/use-gesture-react.esm.cjs');

const RESPONSIVE_DEFAULT_SIZE = 800;
const ResizableWrapper = ({
  children,
  isResizable,
  onDraggingChange,
  onSizeChange
}) => {
  const [dragging, setDragging] = React.useState(false);
  const [responsiveSize, setResponsiveSize] = React.useState(null);
  const initialSizeRef = React.useRef({ width: 0, height: 0 });
  React.useLayoutEffect(() => {
    if (isResizable) {
      const initialWidth = RESPONSIVE_DEFAULT_SIZE;
      const initialHeight = RESPONSIVE_DEFAULT_SIZE;
      setResponsiveSize({ width: initialWidth, height: initialHeight });
      onSizeChange?.(initialWidth, initialHeight);
    }
  }, [isResizable, onSizeChange]);
  const handleSetDragging = React.useCallback(
    (value) => {
      setDragging(value);
      onDraggingChange?.(value);
    },
    [onDraggingChange]
  );
  const horizontalDragConfig = React.useMemo(() => {
    return {
      axis: "x",
      from: () => [0, 0],
      filterTaps: true
    };
  }, []);
  const verticalDragConfig = React.useMemo(() => {
    return {
      axis: "y",
      from: () => [0, 0],
      filterTaps: true
    };
  }, []);
  const bindHorizontalResizer = useGestureReact_esm.useDrag(({ down, movement: [mx], first, last }) => {
    if (first) {
      initialSizeRef.current = {
        width: responsiveSize?.width || RESPONSIVE_DEFAULT_SIZE,
        height: responsiveSize?.height || RESPONSIVE_DEFAULT_SIZE
      };
      handleSetDragging(true);
    }
    if (down) {
      const newWidth = Math.max(320, initialSizeRef.current.width + mx);
      const newHeight = initialSizeRef.current.height;
      setResponsiveSize({ width: newWidth, height: newHeight });
      onSizeChange?.(newWidth, newHeight);
    }
    if (last) {
      setTimeout(() => {
        handleSetDragging(false);
      }, 0);
    }
  }, horizontalDragConfig);
  const bindVerticalResizer = useGestureReact_esm.useDrag(({ down, movement: [, my], first, last }) => {
    if (first) {
      initialSizeRef.current = {
        width: responsiveSize?.width || RESPONSIVE_DEFAULT_SIZE,
        height: responsiveSize?.height || RESPONSIVE_DEFAULT_SIZE
      };
      handleSetDragging(true);
    }
    if (down) {
      const newWidth = initialSizeRef.current.width;
      const newHeight = Math.max(200, initialSizeRef.current.height + my);
      setResponsiveSize({ width: newWidth, height: newHeight });
      onSizeChange?.(newWidth, newHeight);
    }
    if (last) {
      setTimeout(() => {
        handleSetDragging(false);
      }, 0);
    }
  }, verticalDragConfig);
  const bindHorizontalResizerValues = React.useMemo(() => {
    return typeof bindHorizontalResizer === "function" ? bindHorizontalResizer() : {};
  }, [bindHorizontalResizer]);
  const bindVerticalResizerValues = React.useMemo(() => {
    return typeof bindVerticalResizer === "function" ? bindVerticalResizer() : {};
  }, [bindVerticalResizer]);
  const responsiveStyle = React.useMemo(() => {
    if (isResizable && responsiveSize) {
      return {
        width: `${responsiveSize.width}px`,
        height: `${responsiveSize.height}px`
      };
    }
    return { width: "100%" };
  }, [isResizable, responsiveSize]);
  const contextValue = React.useMemo(() => ({
    dragging,
    setDragging: handleSetDragging
  }), [dragging, handleSetDragging]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    DragHandleContext.Provider,
    {
      value: contextValue,
      children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "relative", style: responsiveStyle, children: [
        isResizable && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            Resizer,
            {
              ...bindHorizontalResizerValues,
              className: "absolute top-1/2 right-[-20px] -translate-y-1/2",
              children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.GripVertical, { className: "w-4 h-4" })
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(
            Resizer,
            {
              ...bindVerticalResizerValues,
              className: "absolute bottom-[-20px] left-1/2 -translate-x-1/2 cursor-ns-resize",
              children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.GripVertical, { className: "w-4 h-4 rotate-90" })
            }
          )
        ] }),
        children
      ] })
    }
  );
};
const DragHandleContext = React.createContext({ dragging: false, setDragging: () => {
} });
const Resizer = ({
  className,
  children,
  ...props
}) => {
  const handleMouseDown = React.useCallback((e) => {
    e.stopPropagation();
    e.preventDefault();
  }, []);
  const handleTouchStart = React.useCallback(
    (e) => {
      e.stopPropagation();
    },
    []
  );
  return /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    {
      "data-testid": "resizer",
      onMouseDown: handleMouseDown,
      onTouchStart: handleTouchStart,
      className: utils.cn(
        "flex items-center justify-center w-4 h-4 rounded-sm border bg-border hover:bg-muted touch-none z-[1001]",
        // Default cursor is ew-resize, but can be overridden by className
        !className?.includes("cursor-") && "cursor-ew-resize",
        className
      ),
      ...props,
      children
    }
  );
};

exports.DragHandleContext = DragHandleContext;
exports.ResizableWrapper = ResizableWrapper;
