import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "disabled", "element", "icon", "size", "color", "tint", "selected", "type", "onMouseUp", "onMouseLeave", "processing"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import React, { useRef, useImperativeHandle, forwardRef } from 'react';
import PropTypes from 'prop-types';
import Box from '../box';
import Icon from '../icon';
import cx from 'classnames';
import buttonTheme from '../button/theme.css';
import theme from './theme.css';
import LoadingSpinner from '../loadingSpinner';
/** @type {React.ComponentType<any>} */

var IconButton = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _cx;

  var children = _ref.children,
      className = _ref.className,
      disabled = _ref.disabled,
      element = _ref.element,
      icon = _ref.icon,
      size = _ref.size,
      color = _ref.color,
      tint = _ref.tint,
      selected = _ref.selected,
      type = _ref.type,
      onMouseUp = _ref.onMouseUp,
      onMouseLeave = _ref.onMouseLeave,
      processing = _ref.processing,
      others = _objectWithoutProperties(_ref, _excluded);

  var buttonRef = useRef();
  useImperativeHandle(ref, function () {
    return buttonRef.current;
  });

  var handleMouseUp = function handleMouseUp(event) {
    blur();

    if (onMouseUp) {
      onMouseUp(event);
    }
  };

  var handleMouseLeave = function handleMouseLeave(event) {
    blur();

    if (onMouseLeave) {
      onMouseLeave(event);
    }
  };

  var blur = function blur() {
    var currentButtonRef = buttonRef.current;

    if (currentButtonRef.blur) {
      currentButtonRef.blur();
    }
  };

  var classNames = cx(buttonTheme['button-base'], theme['icon-button'], theme["is-".concat(size)], (_cx = {}, _defineProperty(_cx, theme['is-disabled'], disabled), _defineProperty(_cx, theme['is-processing'], processing), _defineProperty(_cx, theme['is-selected'], selected), _cx), className);

  var props = _objectSpread(_objectSpread({}, others), {}, {
    ref: buttonRef,
    className: classNames,
    disabled: element === 'button' ? disabled : null,
    element: element,
    onMouseUp: handleMouseUp,
    onMouseLeave: handleMouseLeave,
    type: element === 'button' ? type : null,
    'data-teamleader-ui': 'icon-button'
  });

  if (processing) {
    return /*#__PURE__*/React.createElement(Box, props, /*#__PURE__*/React.createElement(LoadingSpinner, {
      className: theme['spinner'],
      color: color,
      size: size === 'small' ? 'small' : 'medium',
      tint: tint
    }));
  }

  return /*#__PURE__*/React.createElement(Box, props, /*#__PURE__*/React.createElement(Icon, {
    color: color === 'white' ? 'neutral' : color,
    tint: color === 'white' ? 'lightest' : tint
  }, icon), children);
});
IconButton.defaultProps = {
  className: '',
  element: 'button',
  size: 'medium',
  color: 'neutral',
  processing: false,
  tint: 'darkest',
  type: 'button'
};
IconButton.displayName = 'IconButton';
export default IconButton;