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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { ref } from 'airbnb-prop-types';
import Icon from '../../../base/Icon';
import './style.css';

var ButtonWithIcon = function ButtonWithIcon(props) {
  var _classNames;

  var classes = props.classes,
      iconSize = props.iconSize,
      icon = props.icon,
      expanded = props.expanded,
      capitalized = props.capitalized,
      usage = props.usage,
      theme = props.theme,
      setButtonRef = props.setButtonRef,
      text = props.text,
      size = props.size,
      rest = _objectWithoutProperties(props, ['classes', 'iconSize', 'icon', 'expanded', 'capitalized', 'usage', 'theme', 'setButtonRef', 'text', 'size']);

  var buttonClasses = classNames((_classNames = {
    'ma__button-icon': true
  }, _classNames['ma__button-icon--' + size] = size, _classNames['ma__button-icon--expanded'] = expanded, _classNames['ma__button-icon--capitalized'] = capitalized, _classNames['ma__icon-small'] = iconSize === 'small' || icon.props.name === 'chevron', _classNames['ma__button-search'] = icon.props.name === 'search', _classNames['ma__button-search--secondary'] = icon.props.name === 'search' && usage === 'secondary', _classNames['ma__button-icon--' + theme] = theme, _classNames['ma__button-icon--' + usage] = usage, _classNames[classes.join(' ')] = classes, _classNames));
  var buttonProps = _extends({}, rest, {
    className: buttonClasses,
    tabIndex: 0
  });
  return React.createElement(
    'button',
    _extends({}, buttonProps, { ref: setButtonRef, 'aria-expanded': expanded }),
    React.createElement(
      'span',
      null,
      text
    ),
    icon
  );
};

ButtonWithIcon.propTypes = process.env.NODE_ENV !== "production" ? {
  /** id for the button */
  id: PropTypes.string,
  // Function to run on click of the button.
  onClick: PropTypes.func,
  // Sets a reference to the button onto the passed node.
  setButtonRef: ref(),
  // Button text.
  text: PropTypes.string,
  /** HTML <button> 'type' attribute  */
  type: PropTypes.oneOf(['submit', 'reset', 'button', '']),
  /** Create a smaller button */
  size: PropTypes.oneOf(['', 'small', 'large']),
  // Button classes.
  classes: PropTypes.arrayOf(PropTypes.string),
  // Icon to display within the button.
  icon: PropTypes.element,
  // Adds expanded class to button if true.
  expanded: PropTypes.bool,
  // Adds capitalized class to button if true.
  capitalized: PropTypes.bool,
  // Defines the size, default size fits the most square icons and "small" setting is specific for the chevron icon.
  iconSize: PropTypes.oneOf(['', 'small']),
  /** The aria-label property is used to provide the label to any assistive
   * technologies. This is useful if the text value is not descriptive of the
   * button's functionality. */
  'aria-label': PropTypes.string,
  /** Themes correspond to site color scheme i.e. sass variables */
  theme: PropTypes.oneOf(['', 'c-primary', 'c-highlight', 'c-gray-dark', 'c-black']),
  /** Button usage */
  usage: PropTypes.oneOf(['', 'secondary', 'tertiary', 'quaternary', 'quaternary-simple', 'alert']),
  /** Whether the button has a popup or not */
  'aria-haspopup': PropTypes.bool
} : {};

ButtonWithIcon.defaultProps = {
  text: 'Search',
  type: 'submit',
  size: '',
  classes: [],
  icon: React.createElement(Icon, { name: 'search', svgHeight: 20, svgWidth: 20 }),
  capitalized: false,
  iconSize: '',
  'aria-label': '',
  usage: '',
  theme: '',
  'aria-haspopup': false
};

export default ButtonWithIcon;