import React, { useRef } from "react";
import { Resizable as ReResizable } from "re-resizable";
import { classes } from "../../Popup.tokens";
import { getHandleStyles, getRatioBasedOnPlacement, getResizeDirections } from "../../utils";
import { IconResizeDiagonalStyled } from "../../Popup.styles";
export const Resizable = ({ children, resizable: outerResizable, placement })=>{
    const resizableContainer = useRef(null);
    const resizable = typeof outerResizable === 'boolean' ? {
        disabled: !outerResizable
    } : outerResizable;
    const handleResizeStart = ()=>{
        if (resizableContainer?.current && resizableContainer.current.resizable) {
            resizableContainer.current.resizable.classList.toggle(classes.resizableContainerNoSelect);
        }
        if (resizable?.onResizeStart) {
            resizable.onResizeStart(resizableContainer);
        }
    };
    const handleResizeStop = ()=>{
        if (resizableContainer?.current && resizableContainer.current.resizable) {
            resizableContainer.current.resizable.classList.toggle(classes.resizableContainerNoSelect);
        }
        if (resizable?.onResizeEnd) {
            resizable.onResizeEnd(resizableContainer);
        }
    };
    if (!resizable || resizable.disabled) {
        return /*#__PURE__*/ React.createElement(React.Fragment, null, children);
    }
    return /*#__PURE__*/ React.createElement(ReResizable, {
        ref: resizableContainer,
        enable: resizable && !resizable.disabled ? getResizeDirections(resizable.directions) : false,
        resizeRatio: getRatioBasedOnPlacement(placement),
        defaultSize: resizable?.defaultSize,
        minWidth: resizable?.minWidth,
        minHeight: resizable?.minHeight,
        maxWidth: resizable?.maxWidth,
        maxHeight: resizable?.maxHeight,
        onResizeStart: handleResizeStart,
        onResizeStop: handleResizeStop,
        handleComponent: {
            topRight: resizable?.hiddenIcons?.includes('top-right') ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, resizable?.icons?.topRight || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableTopRightIcon,
                color: "inherit",
                size: resizable?.iconSize
            })),
            bottomRight: resizable?.hiddenIcons?.includes('bottom-right') ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, resizable?.icons?.bottomRight || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableBottomRightIcon,
                color: "inherit",
                size: resizable?.iconSize
            })),
            bottomLeft: resizable?.hiddenIcons?.includes('bottom-left') ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, resizable?.icons?.bottomLeft || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableBottomLeftIcon,
                color: "inherit",
                size: resizable?.iconSize
            })),
            topLeft: resizable?.hiddenIcons?.includes('top-left') ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, resizable?.icons?.topLeft || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableTopLeftIcon,
                color: "inherit",
                size: resizable?.iconSize
            }))
        },
        className: classes.resizableContainer,
        handleStyles: getHandleStyles(),
        handleWrapperClass: classes.resizableHandleWrapper
    }, children);
};
