function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import PropTypes from "prop-types";
import React from "react";
import Icon from "react-oui-icons";
import classNames from "classnames";
import { FILL_COLOR_MAP } from "../../utils/accessibility";

var ButtonIcon = function ButtonIcon(_ref) {
  var buttonRef = _ref.buttonRef,
      className = _ref.className,
      iconFill = _ref.iconFill,
      iconName = _ref.iconName,
      isDisabled = _ref.isDisabled,
      onClick = _ref.onClick,
      size = _ref.size,
      style = _ref.style,
      testSection = _ref.testSection,
      title = _ref.title,
      props = _objectWithoutProperties(_ref, ["buttonRef", "className", "iconFill", "iconName", "isDisabled", "onClick", "size", "style", "testSection", "title"]);

  // ensure valid fillColor name (in the case that propType errors are ignored)
  var fillColor = iconFill && Object.keys(FILL_COLOR_MAP).includes(iconFill) ? FILL_COLOR_MAP[iconFill] : null;

  function handleOnClick(event) {
    if (isDisabled) {
      return;
    }

    onClick && onClick(event);
  }

  return React.createElement("button", _extends({
    className: classNames("oui-button oui-button-icon oui-button-icon__".concat(size, " oui-button--").concat(style), className),
    "data-oui-component": true,
    "data-test-section": testSection,
    disabled: isDisabled,
    onClick: handleOnClick,
    ref: buttonRef,
    title: title,
    type: "button"
  }, props), React.createElement("div", {
    className: "flex flex--dead-center "
  }, React.createElement(Icon, {
    fill: fillColor,
    name: iconName,
    size: size,
    title: title
  })));
};

ButtonIcon.propTypes = {
  /** React ref to the underlying button component */
  buttonRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
    current: PropTypes.instanceOf(Element)
  })]),

  /** CSS class names. */
  className: PropTypes.string,

  /**
   *  Color to use for the fill of the icon
   */
  iconFill: PropTypes.oneOf(Object.keys(FILL_COLOR_MAP)),

  /**
   *  Name of the icon to use
   */
  iconName: PropTypes.string.isRequired,

  /**
   * Prevent users from interacting with the button
   */
  isDisabled: PropTypes.bool,

  /**
   *  Function to perform when the close button is clicked.
   */
  onClick: PropTypes.func,

  /**
   *  Size of the button, medium by default
   */
  size: PropTypes.oneOf(["small", "medium", "large"]),

  /**
   *  Various style options
   */
  style: PropTypes.oneOf(["highlight", "danger", "danger-outline", "outline", "plain", "unstyled"]),

  /**
   * Hook for automated JavaScript tests
   */
  testSection: PropTypes.string,

  /**
   *  Title of the button used on hover and for screen readers
   */
  title: PropTypes.string.isRequired
};
ButtonIcon.defaultProps = {
  iconName: "add",
  onClick: function onClick() {
    return null;
  },
  size: "medium",
  title: ""
};
export default React.memo(ButtonIcon);