"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

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

var _js = require("../../globals/js");

var _excluded = ["children", "className", "disabled", "small", "large", "kind", "allCaps", "href", "iconReverse", "tabIndex", "type", "useFlexbox", "icon", "iconDescription", "onClick"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var prefix = _js.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 = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useState = (0, _react.useState)(0),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      count = _useState2[0],
      setCount = _useState2[1];

  (0, _react.useEffect)(function () {
    var timer = setTimeout(function () {
      return endAnimation();
    }, 500);
    return function () {
      clearTimeout(timer);
    };
  }, [count]);
  var buttonClasses = (0, _classnames.default)(className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn"), true), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--sm"), small), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--lg"), large), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--icon-reverse"), iconReverse), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--flexbox"), useFlexbox), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--icon-only"), icon && children === undefined), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--primary"), kind === 'primary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--danger"), kind === 'danger'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--accent"), kind === 'accent'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--secondary"), kind === 'secondary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--navigation"), kind === 'navigation'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--ghost"), kind === 'ghost'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--inverse--primary"), kind === 'inverse--primary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--inverse"), kind === 'inverse'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--danger--primary"), kind === 'danger--primary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--danger--secondary"), kind === 'danger--secondary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--tertiary"), kind === 'tertiary'), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--animating"), count), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--btn--all-caps"), allCaps), _classNames));
  var commonProps = {
    tabIndex: tabIndex,
    className: buttonClasses
  };
  var Icon = icon;
  var buttonImage = icon && /*#__PURE__*/_react.default.isValidElement(icon) ? /*#__PURE__*/_react.default.createElement("span", {
    className: "".concat(prefix, "--btn__icon")
  }, icon) : icon ? /*#__PURE__*/_react.default.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.default.createElement("button", (0, _extends2.default)({}, other, commonProps, {
    disabled: disabled,
    type: type,
    onClick: onClickAnimation,
    ref: other.inputref
  }), iconReverse && buttonImage, children, !iconReverse && buttonImage);

  var anchor = /*#__PURE__*/_react.default.createElement("a", (0, _extends2.default)({}, 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.default.node,

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

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

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

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

  /**
   * FOR DESIGNERS
   * Specify the kind of Button you want to create
   */
  kind: _propTypes.default.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.default.string,

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

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

  /**
   * Optional prop to specify the role of the Button
   */
  role: _propTypes.default.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.default.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.default.bool
};
Button.defaultProps = {
  iconReverse: false,
  iconDescription: 'Provide icon description if icon is used',
  tabIndex: 0,
  type: 'button',
  disabled: false,
  small: false,
  kind: 'primary'
};
var _default = Button;
exports.default = _default;