"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_LITE_VARIANT = exports.BUTTON_LITE_SIZE = void 0;

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

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

var _Text = _interopRequireDefault(require("../text/Text"));

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

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

var _excluded = ["className", "variant", "children", "size", "loading", "loadingAriaLabel", "loadingAriaLive", "disabled", "icon", "reversedOrder", "href", "target", "newTabLabel", "type", "onClick", "aria-label"];

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

var BUTTON_LITE_SIZE = {
  XS: 'xs',
  S: 's'
};
exports.BUTTON_LITE_SIZE = BUTTON_LITE_SIZE;
var BUTTON_LITE_VARIANT = {
  TRANSPARENT: 'transparent',
  TRANSPARENT_INVERTED: 'transparent-inverted',
  TRANSPARENT_LIGHT: 'transparent-light',
  TRANSPARENT_INDIGO: 'transparent-indigo',
  SOLID_LIGHT: 'solid-light',
  SOLID_INDIGO_LIGHT: 'solid-indigo-light'
};
exports.BUTTON_LITE_VARIANT = BUTTON_LITE_VARIANT;
var anchorRelatedProps = ['download', 'hreflang', 'ping', 'referrerpolicy', 'rel'];

var ButtonLite = function ButtonLite(_ref) {
  var className = _ref.className,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? BUTTON_LITE_VARIANT.TRANSPARENT : _ref$variant,
      children = _ref.children,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? BUTTON_LITE_SIZE.S : _ref$size,
      loading = _ref.loading,
      loadingAriaLabel = _ref.loadingAriaLabel,
      loadingAriaLive = _ref.loadingAriaLive,
      disabled = _ref.disabled,
      icon = _ref.icon,
      reversedOrder = _ref.reversedOrder,
      href = _ref.href,
      target = _ref.target,
      _ref$newTabLabel = _ref.newTabLabel,
      newTabLabel = _ref$newTabLabel === void 0 ? '(opens in a new tab)' : _ref$newTabLabel,
      type = _ref.type,
      onClick = _ref.onClick,
      ariaLabel = _ref['aria-label'],
      rest = _objectWithoutProperties(_ref, _excluded);

  var buttonClass = (0, _classnames.default)('sg-button-lite', "sg-button-lite--".concat(variant), "sg-button-lite--".concat(size), {
    'sg-button-lite--loading': loading,
    'sg-button-lite--reversed-order': reversedOrder
  }, className);
  var isDisabled = disabled || loading;
  var isLink = !!href;
  var hasIcon = icon !== undefined && icon !== null;

  if (_utils.__DEV__) {
    (0, _utils.invariant)(!(reversedOrder && !icon), "Using 'reversedOrder' property has no effect when 'icon' property is not set.");
    (0, _utils.invariant)(!(!isLink && (target || Object.keys(rest).some(function (p) {
      return anchorRelatedProps.includes(p);
    }))), "An anchor-related prop is not working when \"href\" is not provided: ".concat(Object.keys(rest)));
    (0, _utils.invariant)(!(isLink && type), '`type` prop is not working when href is provided');
  }

  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({}, rest, {
    className: buttonClass,
    disabled: isDisabled,
    "aria-label": ariaLabel,
    type: type,
    target: target,
    href: href,
    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;
    }
  }), loading && /*#__PURE__*/React.createElement(_Spinner.default, {
    "aria-live": loadingAriaLive,
    "aria-label": loadingAriaLabel,
    className: "sg-button-lite__spinner",
    size: size === 'xs' ? 'xxsmall' : 'xsmall'
  }), hasIcon && /*#__PURE__*/React.createElement("span", {
    className: "sg-button-lite__icon"
  }, icon), /*#__PURE__*/React.createElement(_Text.default, {
    className: "sg-button-lite__text",
    weight: "bold",
    size: "small"
  }, children, target === '_blank' && /*#__PURE__*/React.createElement("span", {
    className: "sg-visually-hidden"
  }, newTabLabel)));
};

var _default = ButtonLite;
exports.default = _default;