import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "disabled", "small", "large", "kind", "allCaps", "href", "iconReverse", "tabIndex", "type", "useFlexbox", "icon", "iconDescription", "onClick"];
import PropTypes from 'prop-types';
import React, { useEffect, useState } from 'react';
import classNames from 'classnames';
import { settings } from '../../globals/js';
var prefix = settings.prefix;
/**
 * Buttons express what action will occur when the user clicks or touches it. Buttons are used to initialize an action, either in the background or foreground of an experience. */

var Button = function Button(_ref) {
  var _classNames;

  var children = _ref.children,
      className = _ref.className,
      disabled = _ref.disabled,
      small = _ref.small,
      large = _ref.large,
      kind = _ref.kind,
      allCaps = _ref.allCaps,
      href = _ref.href,
      iconReverse = _ref.iconReverse,
      tabIndex = _ref.tabIndex,
      type = _ref.type,
      useFlexbox = _ref.useFlexbox,
      icon = _ref.icon,
      iconDescription = _ref.iconDescription,
      onClick = _ref.onClick,
      other = _objectWithoutProperties(_ref, _excluded);

  var _useState = useState(0),
      _useState2 = _slicedToArray(_useState, 2),
      count = _useState2[0],
      setCount = _useState2[1];

  useEffect(function () {
    var timer = setTimeout(function () {
      return endAnimation();
    }, 500);
    return function () {
      clearTimeout(timer);
    };
  }, [count]);
  var buttonClasses = classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--btn"), true), _defineProperty(_classNames, "".concat(prefix, "--btn--sm"), small), _defineProperty(_classNames, "".concat(prefix, "--btn--lg"), large), _defineProperty(_classNames, "".concat(prefix, "--btn--icon-reverse"), iconReverse), _defineProperty(_classNames, "".concat(prefix, "--btn--flexbox"), useFlexbox), _defineProperty(_classNames, "".concat(prefix, "--btn--icon-only"), icon && children === undefined), _defineProperty(_classNames, "".concat(prefix, "--btn--primary"), kind === 'primary'), _defineProperty(_classNames, "".concat(prefix, "--btn--danger"), kind === 'danger'), _defineProperty(_classNames, "".concat(prefix, "--btn--accent"), kind === 'accent'), _defineProperty(_classNames, "".concat(prefix, "--btn--secondary"), kind === 'secondary'), _defineProperty(_classNames, "".concat(prefix, "--btn--navigation"), kind === 'navigation'), _defineProperty(_classNames, "".concat(prefix, "--btn--ghost"), kind === 'ghost'), _defineProperty(_classNames, "".concat(prefix, "--btn--inverse--primary"), kind === 'inverse--primary'), _defineProperty(_classNames, "".concat(prefix, "--btn--inverse"), kind === 'inverse'), _defineProperty(_classNames, "".concat(prefix, "--btn--danger--primary"), kind === 'danger--primary'), _defineProperty(_classNames, "".concat(prefix, "--btn--danger--secondary"), kind === 'danger--secondary'), _defineProperty(_classNames, "".concat(prefix, "--btn--tertiary"), kind === 'tertiary'), _defineProperty(_classNames, "".concat(prefix, "--btn--animating"), count), _defineProperty(_classNames, "".concat(prefix, "--btn--all-caps"), allCaps), _classNames));
  var commonProps = {
    tabIndex: tabIndex,
    className: buttonClasses
  };
  var Icon = icon;
  var buttonImage = icon && /*#__PURE__*/React.isValidElement(icon) ? /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefix, "--btn__icon")
  }, icon) : icon ? /*#__PURE__*/React.createElement(Icon, {
    description: iconDescription,
    className: "".concat(prefix, "--btn__icon")
  }) : null;

  var endAnimation = function endAnimation() {
    setCount(false);
  };

  var onClickAnimation = function onClickAnimation(e) {
    if (onClick) {
      onClick(e);
    }

    setCount(true);
  };

  var button = /*#__PURE__*/React.createElement("button", _extends({}, other, commonProps, {
    disabled: disabled,
    type: type,
    onClick: onClickAnimation,
    ref: other.inputref
  }), iconReverse && buttonImage, children, !iconReverse && buttonImage);
  var anchor = /*#__PURE__*/React.createElement("a", _extends({}, other, commonProps, {
    href: href,
    role: "button",
    onClick: onClickAnimation,
    ref: other.inputref
  }), children, buttonImage);
  return href ? anchor : button;
};

Button.propTypes = {
  /**
   * Specify the content of your Button
   */
  children: PropTypes.node,

  /**
   * Specify an optional className to be added to your Button
   */
  className: PropTypes.string,

  /**
   * Specify whether the Button should be disabled, or not
   */
  disabled: PropTypes.bool,

  /**
   * FOR DESIGNERS
   * Specify whether the Button should be a small variant
   */
  small: PropTypes.bool,

  /**
   * FOR DESIGNERS
   * Specify whether the Button should be a large variant
   */
  large: PropTypes.bool,

  /**
   * FOR DESIGNERS
   * Specify the kind of Button you want to create
   */
  kind: PropTypes.oneOf(['primary', 'secondary', 'accent', 'danger', 'ghost', 'inverse--primary', 'inverse', 'danger--primary', 'tertiary', 'navigation']),
  //ButtonTypes.buttonKind.isRequired,

  /**
   * Optionally specify an href for your Button to become an <a> element
   */
  href: PropTypes.string,

  /**
   * Optional prop to specify the tabIndex of the Button
   */
  tabIndex: PropTypes.number,

  /**
   * Optional prop to specify the type of the Button
   */
  type: PropTypes.oneOf(['button', 'reset', 'submit']),

  /**
   * Optional prop to specify the role of the Button
   */
  role: PropTypes.string,

  /**
   * Specify an `icon` to include in the Button through an object representing the SVG data of the icon, similar to the `Icon` component
   */
  icon: PropTypes.component,

  /**
   * If specifying the `icon` prop, provide a description for that icon that can
   * be read by screen readers
   */
  iconDescription: function iconDescription(props) {
    if (props.icon && !props.iconDescription) {
      return new Error('icon property specified without also providing an iconDescription property.');
    }

    return undefined;
  },

  /**
   * Optionally specify an href for your Button to become an <a> element
   */
  iconReverse: PropTypes.bool
};
Button.defaultProps = {
  iconReverse: false,
  iconDescription: 'Provide icon description if icon is used',
  tabIndex: 0,
  type: 'button',
  disabled: false,
  small: false,
  kind: 'primary'
};
export default Button;