"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireDefault(require("react"));

var _reactOuiIcons = _interopRequireDefault(require("react-oui-icons"));

var _classnames = _interopRequireDefault(require("classnames"));

var _accessibility = require("../../utils/accessibility");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

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(_accessibility.FILL_COLOR_MAP).includes(iconFill) ? _accessibility.FILL_COLOR_MAP[iconFill] : null;

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

    onClick && onClick(event);
  }

  return _react["default"].createElement("button", _extends({
    className: (0, _classnames["default"])("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["default"].createElement("div", {
    className: "flex flex--dead-center "
  }, _react["default"].createElement(_reactOuiIcons["default"], {
    fill: fillColor,
    name: iconName,
    size: size,
    title: title
  })));
};

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

  /** CSS class names. */
  className: _propTypes["default"].string,

  /**
   *  Color to use for the fill of the icon
   */
  iconFill: _propTypes["default"].oneOf(Object.keys(_accessibility.FILL_COLOR_MAP)),

  /**
   *  Name of the icon to use
   */
  iconName: _propTypes["default"].string.isRequired,

  /**
   * Prevent users from interacting with the button
   */
  isDisabled: _propTypes["default"].bool,

  /**
   *  Function to perform when the close button is clicked.
   */
  onClick: _propTypes["default"].func,

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

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

  /**
   * Hook for automated JavaScript tests
   */
  testSection: _propTypes["default"].string,

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

var _default = _react["default"].memo(ButtonIcon);

exports["default"] = _default;