import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["size", "variant", "icon", "iconOnly", "reversedOrder", "href", "fullWidth", "disabled", "loading", "toggle", "children", "className", "target", "newTabLabel", "onClick", "aria-label", "loadingAriaLive", "loadingAriaLabel", "type"];

var _SPINNER_SIZE_MAP, _SPINNER_COLOR_MAP;

import * as React from 'react';
import Spinner, { SPINNER_SIZE, SPINNER_COLOR } from '../spinner/Spinner';
import cx from 'classnames';
import { __DEV__, invariant } from '../utils';
export var BUTTON_SIZE = {
  L: 'l',
  M: 'm',
  S: 's'
};
export var BUTTON_VARIANT = {
  SOLID: 'solid',
  SOLID_INVERTED: 'solid-inverted',
  SOLID_INDIGO: 'solid-indigo',
  SOLID_INDIGO_INVERTED: 'solid-indigo-inverted',
  SOLID_LIGHT: 'solid-light',
  OUTLINE: 'outline',
  OUTLINE_INDIGO: 'outline-indigo',
  OUTLINE_INVERTED: 'outline-inverted',
  TRANSPARENT: 'transparent',
  TRANSPARENT_LIGHT: 'transparent-light',
  TRANSPARENT_RED: 'transparent-red',
  TRANSPARENT_INVERTED: 'transparent-inverted',
  FACEBOOK: 'facebook',
  GOOGLE: 'google',
  APPLE: 'apple'
};
export var BUTTON_TOGGLE = {
  RED: 'red',
  YELLOW: 'yellow'
};
var SPINNER_SIZE_MAP = (_SPINNER_SIZE_MAP = {}, _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.L, SPINNER_SIZE.SMALL), _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.M, SPINNER_SIZE.XSMALL), _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.S, SPINNER_SIZE.XXSMALL), _SPINNER_SIZE_MAP);
var SPINNER_COLOR_MAP = (_SPINNER_COLOR_MAP = {}, _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID, SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INVERTED, SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INDIGO, SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INDIGO_INVERTED, SPINNER_COLOR['indigo-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_LIGHT, SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE, SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE_INDIGO, SPINNER_COLOR['indigo-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE_INVERTED, SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT, SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_LIGHT, SPINNER_COLOR['gray-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_RED, SPINNER_COLOR['red-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_INVERTED, SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.FACEBOOK, SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.GOOGLE, SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.APPLE, SPINNER_COLOR['white']), _SPINNER_COLOR_MAP);
var TOGGLE_BUTTON_VARIANTS = ['solid-light', 'outline', 'transparent', 'transparent-light'];
var anchorRelatedProps = ['download', 'hreflang', 'ping', 'referrerpolicy', 'rel'];
var Button = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _cx;

  var _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'm' : _ref$size,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? 'solid' : _ref$variant,
      icon = _ref.icon,
      iconOnly = _ref.iconOnly,
      reversedOrder = _ref.reversedOrder,
      href = _ref.href,
      fullWidth = _ref.fullWidth,
      disabled = _ref.disabled,
      loading = _ref.loading,
      toggle = _ref.toggle,
      children = _ref.children,
      className = _ref.className,
      target = _ref.target,
      _ref$newTabLabel = _ref.newTabLabel,
      newTabLabel = _ref$newTabLabel === void 0 ? '(opens in a new tab)' : _ref$newTabLabel,
      onClick = _ref.onClick,
      ariaLabel = _ref['aria-label'],
      _ref$loadingAriaLive = _ref.loadingAriaLive,
      loadingAriaLive = _ref$loadingAriaLive === void 0 ? 'off' : _ref$loadingAriaLive,
      loadingAriaLabel = _ref.loadingAriaLabel,
      type = _ref.type,
      props = _objectWithoutProperties(_ref, _excluded);

  var isDisabled = disabled || loading;
  var isLink = !!href;

  if (__DEV__) {
    invariant(!(toggle === 'red' && ![].concat(TOGGLE_BUTTON_VARIANTS, ['transparent-red']).includes(variant) || toggle === 'yellow' && ![].concat(TOGGLE_BUTTON_VARIANTS).includes(variant)), "Value of toggle property '".concat(String(toggle), "' has no effect when button variant is set to '").concat(variant, "'."));
    invariant(!(iconOnly && !icon), "Using 'iconOnly' property has no effect when 'icon' property is not set.");
    invariant(!(reversedOrder && !icon), "Using 'reversedOrder' property has no effect when 'icon' property is not set.");
    invariant(!(iconOnly && reversedOrder), "Using 'reversedOrder' property has no effect when 'iconOnly' property is set.");
    invariant(!(!isLink && (target || Object.keys(props).some(function (p) {
      return anchorRelatedProps.includes(p);
    }))), // $FlowFixMe
    "An anchor-related prop is not working when \"href\" is not provided: ".concat(Object.keys(props)));
    invariant(!(isLink && type), '`type` prop is not working when href is provided');
    invariant(!(iconOnly && !ariaLabel), 'Using `iconOnly` without `aria-label` will affect people with visual impairments');
  }

  var btnClass = cx('sg-button', (_cx = {}, _defineProperty(_cx, "sg-button--".concat(String(size)), size), _defineProperty(_cx, "sg-button--".concat(String(variant)), variant), _defineProperty(_cx, 'sg-button--disabled', isDisabled), _defineProperty(_cx, 'sg-button--loading', loading), _defineProperty(_cx, 'sg-button--full-width', fullWidth), _defineProperty(_cx, 'sg-button--icon-only', Boolean(icon) && iconOnly), _defineProperty(_cx, "sg-button--".concat(String(variant), "-toggle-").concat(String(toggle)), toggle), _defineProperty(_cx, 'sg-button--reversed-order', reversedOrder), _cx), className);
  var iconClass = cx('sg-button__icon', _defineProperty({}, "sg-button__icon--".concat(size || ''), size));
  var ico;

  if (icon !== undefined && icon !== null) {
    ico = /*#__PURE__*/React.createElement("span", {
      className: iconClass
    }, icon);
  }

  var onButtonClick = function onButtonClick(e) {
    if (isLink && isDisabled) {
      return;
    }

    return onClick && onClick(e);
  };

  var TagToRender = isLink ? isDisabled ? 'span' : 'a' : 'button';
  return /*#__PURE__*/React.createElement(TagToRender, _extends({}, props, {
    className: btnClass,
    href: href,
    disabled: isDisabled // @ts-expect-error generics while using React.forwardRef
    ,
    ref: ref,
    target: target,
    "aria-label": ariaLabel,
    onClick: onButtonClick // On iOS the :active pseudo state is triggered only when there is a touch event set on the HTML element
    // and we use active pseudo class to provide haptic feedback.
    ,
    onTouchStart: function onTouchStart() {
      return null;
    },
    type: type
  }), variant.includes('transparent') ? /*#__PURE__*/React.createElement("span", {
    className: "sg-button__hover-overlay"
  }) : null, loading && /*#__PURE__*/React.createElement(Spinner, {
    size: SPINNER_SIZE_MAP[size],
    color: SPINNER_COLOR_MAP[variant],
    className: "sg-button__spinner",
    "aria-live": loadingAriaLive,
    "aria-label": loadingAriaLabel
  }), ico, /*#__PURE__*/React.createElement("span", {
    className: "sg-button__text"
  }, children, target === '_blank' && /*#__PURE__*/React.createElement("span", {
    className: "sg-visually-hidden"
  }, newTabLabel)));
});
Button.displayName = 'Button';
export default Button;