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 var Resizable = function Resizable(param) {
    var children = param.children, outerResizable = param.resizable, placement = param.placement;
    var _resizable_hiddenIcons, _resizable_icons, _resizable_hiddenIcons1, _resizable_icons1, _resizable_hiddenIcons2, _resizable_icons2, _resizable_hiddenIcons3, _resizable_icons3;
    var resizableContainer = useRef(null);
    var resizable = typeof outerResizable === 'boolean' ? {
        disabled: !outerResizable
    } : outerResizable;
    var handleResizeStart = function handleResizeStart() {
        if ((resizableContainer === null || resizableContainer === void 0 ? void 0 : resizableContainer.current) && resizableContainer.current.resizable) {
            resizableContainer.current.resizable.classList.toggle(classes.resizableContainerNoSelect);
        }
        if (resizable === null || resizable === void 0 ? void 0 : resizable.onResizeStart) {
            resizable.onResizeStart(resizableContainer);
        }
    };
    var handleResizeStop = function handleResizeStop() {
        if ((resizableContainer === null || resizableContainer === void 0 ? void 0 : resizableContainer.current) && resizableContainer.current.resizable) {
            resizableContainer.current.resizable.classList.toggle(classes.resizableContainerNoSelect);
        }
        if (resizable === null || resizable === void 0 ? void 0 : 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 === null || resizable === void 0 ? void 0 : resizable.defaultSize,
        minWidth: resizable === null || resizable === void 0 ? void 0 : resizable.minWidth,
        minHeight: resizable === null || resizable === void 0 ? void 0 : resizable.minHeight,
        maxWidth: resizable === null || resizable === void 0 ? void 0 : resizable.maxWidth,
        maxHeight: resizable === null || resizable === void 0 ? void 0 : resizable.maxHeight,
        onResizeStart: handleResizeStart,
        onResizeStop: handleResizeStop,
        handleComponent: {
            topRight: (resizable === null || resizable === void 0 ? void 0 : (_resizable_hiddenIcons = resizable.hiddenIcons) === null || _resizable_hiddenIcons === void 0 ? void 0 : _resizable_hiddenIcons.includes('top-right')) ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, (resizable === null || resizable === void 0 ? void 0 : (_resizable_icons = resizable.icons) === null || _resizable_icons === void 0 ? void 0 : _resizable_icons.topRight) || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableTopRightIcon,
                color: "inherit",
                size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
            })),
            bottomRight: (resizable === null || resizable === void 0 ? void 0 : (_resizable_hiddenIcons1 = resizable.hiddenIcons) === null || _resizable_hiddenIcons1 === void 0 ? void 0 : _resizable_hiddenIcons1.includes('bottom-right')) ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, (resizable === null || resizable === void 0 ? void 0 : (_resizable_icons1 = resizable.icons) === null || _resizable_icons1 === void 0 ? void 0 : _resizable_icons1.bottomRight) || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableBottomRightIcon,
                color: "inherit",
                size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
            })),
            bottomLeft: (resizable === null || resizable === void 0 ? void 0 : (_resizable_hiddenIcons2 = resizable.hiddenIcons) === null || _resizable_hiddenIcons2 === void 0 ? void 0 : _resizable_hiddenIcons2.includes('bottom-left')) ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, (resizable === null || resizable === void 0 ? void 0 : (_resizable_icons2 = resizable.icons) === null || _resizable_icons2 === void 0 ? void 0 : _resizable_icons2.bottomLeft) || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableBottomLeftIcon,
                color: "inherit",
                size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
            })),
            topLeft: (resizable === null || resizable === void 0 ? void 0 : (_resizable_hiddenIcons3 = resizable.hiddenIcons) === null || _resizable_hiddenIcons3 === void 0 ? void 0 : _resizable_hiddenIcons3.includes('top-left')) ? undefined : /*#__PURE__*/ React.createElement(React.Fragment, null, (resizable === null || resizable === void 0 ? void 0 : (_resizable_icons3 = resizable.icons) === null || _resizable_icons3 === void 0 ? void 0 : _resizable_icons3.topLeft) || /*#__PURE__*/ React.createElement(IconResizeDiagonalStyled, {
                className: classes.resizableTopLeftIcon,
                color: "inherit",
                size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
            }))
        },
        className: classes.resizableContainer,
        handleStyles: getHandleStyles(),
        handleWrapperClass: classes.resizableHandleWrapper
    }, children);
};
