'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var reResizable = require('re-resizable');
var Popup_tokens = require('../../Popup.tokens.js');
var Popup_styles = require('../../Popup.styles.js');
var getResizeDirections = require('../../utils/getResizeDirections.js');
var getRatioBasedOnPlacement = require('../../utils/getRatioBasedOnPlacement.js');
var getHandleStyles = require('../../utils/getHandleStyles.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var Resizable = function Resizable(_ref) {
  var _resizable$hiddenIcon, _resizable$icons, _resizable$hiddenIcon2, _resizable$icons2, _resizable$hiddenIcon3, _resizable$icons3, _resizable$hiddenIcon4, _resizable$icons4;
  var children = _ref.children,
    outerResizable = _ref.resizable,
    placement = _ref.placement;
  var resizableContainer = React.useRef(null);
  var resizable = typeof outerResizable === 'boolean' ? {
    disabled: !outerResizable
  } : outerResizable;
  var handleResizeStart = function handleResizeStart() {
    if (resizableContainer !== null && resizableContainer !== void 0 && resizableContainer.current && resizableContainer.current.resizable) {
      resizableContainer.current.resizable.classList.toggle(Popup_tokens.classes.resizableContainerNoSelect);
    }
    if (resizable !== null && resizable !== void 0 && resizable.onResizeStart) {
      resizable.onResizeStart(resizableContainer);
    }
  };
  var handleResizeStop = function handleResizeStop() {
    if (resizableContainer !== null && resizableContainer !== void 0 && resizableContainer.current && resizableContainer.current.resizable) {
      resizableContainer.current.resizable.classList.toggle(Popup_tokens.classes.resizableContainerNoSelect);
    }
    if (resizable !== null && resizable !== void 0 && resizable.onResizeEnd) {
      resizable.onResizeEnd(resizableContainer);
    }
  };
  if (!resizable || resizable.disabled) {
    return /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, children);
  }
  return /*#__PURE__*/React__default.default.createElement(reResizable.Resizable, {
    ref: resizableContainer,
    enable: resizable && !resizable.disabled ? getResizeDirections.getResizeDirections(resizable.directions) : false,
    resizeRatio: getRatioBasedOnPlacement.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 && (_resizable$hiddenIcon = resizable.hiddenIcons) !== null && _resizable$hiddenIcon !== void 0 && _resizable$hiddenIcon.includes('top-right') ? undefined : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, (resizable === null || resizable === void 0 || (_resizable$icons = resizable.icons) === null || _resizable$icons === void 0 ? void 0 : _resizable$icons.topRight) || /*#__PURE__*/React__default.default.createElement(Popup_styles.IconResizeDiagonalStyled, {
        className: Popup_tokens.classes.resizableTopRightIcon,
        color: "inherit",
        size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
      })),
      bottomRight: resizable !== null && resizable !== void 0 && (_resizable$hiddenIcon2 = resizable.hiddenIcons) !== null && _resizable$hiddenIcon2 !== void 0 && _resizable$hiddenIcon2.includes('bottom-right') ? undefined : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, (resizable === null || resizable === void 0 || (_resizable$icons2 = resizable.icons) === null || _resizable$icons2 === void 0 ? void 0 : _resizable$icons2.bottomRight) || /*#__PURE__*/React__default.default.createElement(Popup_styles.IconResizeDiagonalStyled, {
        className: Popup_tokens.classes.resizableBottomRightIcon,
        color: "inherit",
        size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
      })),
      bottomLeft: resizable !== null && resizable !== void 0 && (_resizable$hiddenIcon3 = resizable.hiddenIcons) !== null && _resizable$hiddenIcon3 !== void 0 && _resizable$hiddenIcon3.includes('bottom-left') ? undefined : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, (resizable === null || resizable === void 0 || (_resizable$icons3 = resizable.icons) === null || _resizable$icons3 === void 0 ? void 0 : _resizable$icons3.bottomLeft) || /*#__PURE__*/React__default.default.createElement(Popup_styles.IconResizeDiagonalStyled, {
        className: Popup_tokens.classes.resizableBottomLeftIcon,
        color: "inherit",
        size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
      })),
      topLeft: resizable !== null && resizable !== void 0 && (_resizable$hiddenIcon4 = resizable.hiddenIcons) !== null && _resizable$hiddenIcon4 !== void 0 && _resizable$hiddenIcon4.includes('top-left') ? undefined : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, (resizable === null || resizable === void 0 || (_resizable$icons4 = resizable.icons) === null || _resizable$icons4 === void 0 ? void 0 : _resizable$icons4.topLeft) || /*#__PURE__*/React__default.default.createElement(Popup_styles.IconResizeDiagonalStyled, {
        className: Popup_tokens.classes.resizableTopLeftIcon,
        color: "inherit",
        size: resizable === null || resizable === void 0 ? void 0 : resizable.iconSize
      }))
    },
    className: Popup_tokens.classes.resizableContainer,
    handleStyles: getHandleStyles.getHandleStyles(),
    handleWrapperClass: Popup_tokens.classes.resizableHandleWrapper
  }, children);
};

exports.Resizable = Resizable;
