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 { tagPropType, mapToCssModules } from './Shared/helper.js';

//component - CoreUI / CButton

var CButton = function CButton(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      active = props.active,
      ariaLabel = props['aria-label'],
      block = props.block,
      close = props.close,
      color = props.color,
      size = props.size,
      textHtml = props.textHtml,
      pressed = props.pressed,
      shape = props.shape,
      variant = props.variant,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'innerRef', 'active', 'aria-label', 'block', 'close', 'color', 'size', 'textHtml', 'pressed', 'shape', 'variant']);

  var onClick = function onClick(e) {
    if (props.disabled) {
      e.preventDefault();
      return;
    }
    if (props.onClick) {
      props.onClick(e);
    }
  };

  //render

  if (close && typeof children === 'undefined') {
    children = React.createElement(
      'span',
      { 'aria-hidden': true },
      '\xD7'
    );
  }

  var btnColor = 'btn' + (variant ? '-' + variant : '') + '-' + color;

  var classes = mapToCssModules(classNames(className, { close: close }, close || 'btn', close || btnColor, size ? 'btn-' + size : false, block ? 'btn-block' : false, shape ? 'btn-' + shape : false, pressed ? 'btn-pressed' : false, { 'active': active,
    'disabled': props.disabled }), cssModule);

  if (attributes.href && Tag === 'button') {
    Tag = 'a';
  }

  var defaultAriaLabel = close ? 'Close' : null;

  return React.createElement(Tag, _extends({
    type: Tag === 'button' && attributes.onClick ? 'button' : undefined
  }, attributes, {
    className: classes,
    onClick: onClick,
    'aria-label': ariaLabel || defaultAriaLabel,
    ref: innerRef,
    children: textHtml ? textHtml : children
  }));
};

CButton.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: tagPropType,
  children: PropTypes.node,
  cssModule: PropTypes.object,
  className: PropTypes.string,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  active: PropTypes.bool,
  block: PropTypes.bool,
  shape: PropTypes.string,
  variant: PropTypes.oneOf(['', 'ghost', 'outline']),
  color: PropTypes.string,
  close: PropTypes.bool,
  disabled: PropTypes.bool,
  onClick: PropTypes.func,
  size: PropTypes.string,
  pressed: PropTypes.bool,
  textHtml: PropTypes.string,
  'aria-label': PropTypes.string
} : {};

CButton.defaultProps = {
  tag: 'button'
};

//export
export default CButton;