"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _Spinner = _interopRequireWildcard(require("../spinner/Spinner"));

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

var _utils = require("../utils");

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;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var BUTTON_SIZE = {
  L: 'l',
  M: 'm',
  S: 's'
};
exports.BUTTON_SIZE = BUTTON_SIZE;
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'
};
exports.BUTTON_VARIANT = BUTTON_VARIANT;
var BUTTON_TOGGLE = {
  RED: 'red',
  YELLOW: 'yellow'
};
exports.BUTTON_TOGGLE = BUTTON_TOGGLE;
var SPINNER_SIZE_MAP = (_SPINNER_SIZE_MAP = {}, _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.L, _Spinner.SPINNER_SIZE.SMALL), _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.M, _Spinner.SPINNER_SIZE.XSMALL), _defineProperty(_SPINNER_SIZE_MAP, BUTTON_SIZE.S, _Spinner.SPINNER_SIZE.XXSMALL), _SPINNER_SIZE_MAP);
var SPINNER_COLOR_MAP = (_SPINNER_COLOR_MAP = {}, _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID, _Spinner.SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INVERTED, _Spinner.SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INDIGO, _Spinner.SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_INDIGO_INVERTED, _Spinner.SPINNER_COLOR['indigo-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.SOLID_LIGHT, _Spinner.SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE, _Spinner.SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE_INDIGO, _Spinner.SPINNER_COLOR['indigo-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.OUTLINE_INVERTED, _Spinner.SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT, _Spinner.SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_LIGHT, _Spinner.SPINNER_COLOR['gray-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_RED, _Spinner.SPINNER_COLOR['red-50']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.TRANSPARENT_INVERTED, _Spinner.SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.FACEBOOK, _Spinner.SPINNER_COLOR['white']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.GOOGLE, _Spinner.SPINNER_COLOR['black']), _defineProperty(_SPINNER_COLOR_MAP, BUTTON_VARIANT.APPLE, _Spinner.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 (_utils.__DEV__) {
    (0, _utils.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, "'."));
    (0, _utils.invariant)(!(iconOnly && !icon), "Using 'iconOnly' property has no effect when 'icon' property is not set.");
    (0, _utils.invariant)(!(reversedOrder && !icon), "Using 'reversedOrder' property has no effect when 'icon' property is not set.");
    (0, _utils.invariant)(!(iconOnly && reversedOrder), "Using 'reversedOrder' property has no effect when 'iconOnly' property is set.");
    (0, _utils.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)));
    (0, _utils.invariant)(!(isLink && type), '`type` prop is not working when href is provided');
    (0, _utils.invariant)(!(iconOnly && !ariaLabel), 'Using `iconOnly` without `aria-label` will affect people with visual impairments');
  }

  var btnClass = (0, _classnames.default)('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 = (0, _classnames.default)('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.default, {
    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';
var _default = Button;
exports.default = _default;