function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["className", "disabled", "onClick", "size", "type", "variant", "fullWidth", "to", "rel", "target", "children", "id", "color", "as", "loading", "external", "cms", "fullServerRedirect", "onMouseEnter", "onMouseLeave", "onMouseOver", "delayLoadingSpinnerAnimation", "icon", "iconPlacement", "componentId"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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; }
import React, { useContext } from 'react';
import styled, { useTheme } from 'styled-components';
import { assert } from '../../../../common/utils';
import NormalizedElements from '../../../../common/NormalizedElements';
import TrackingContext from '../../../../common/tracking';
import ButtonContent from '../ButtonContent';
import { neutralStyles, primaryStyles, secondaryStyles, negativeStyles } from './Button.styles';
import { useLink } from '../../../../common/Links';
var isPrimary = function isPrimary(variant) {
  return variant === 'primary';
};
var isSecondary = function isSecondary(variant) {
  return variant === 'secondary';
};
var isNeutral = function isNeutral(variant) {
  return variant === 'neutral';
};
var isNegative = function isNegative(variant) {
  return variant === 'negative';
};
var StyledButton = styled(NormalizedElements.Button).withConfig({
  displayName: "Button__StyledButton",
  componentId: "sc-rtfjm6-0"
})(["", " cursor:", ";"], function (p) {
  if (isSecondary(p.$variant)) {
    return secondaryStyles;
  }
  if (isNeutral(p.$variant)) {
    return neutralStyles;
  }
  if (isNegative(p.$variant)) {
    return negativeStyles;
  }
  return primaryStyles;
}, function (p) {
  return p.disabled ? 'not-allowed' : 'pointer';
});
var CleanLink = function CleanLink(props) {
  var RawLink = useLink();
  return React.createElement(RawLink, props);
};
var StyledLink = styled(CleanLink).withConfig({
  displayName: "Button__StyledLink",
  componentId: "sc-rtfjm6-1"
})(["", " text-decoration:none;&:hover{text-decoration:", ";}"], function (p) {
  if (isSecondary(p.$variant)) {
    return secondaryStyles;
  }
  if (isNeutral(p.$variant)) {
    return neutralStyles;
  }
  return primaryStyles;
}, function (p) {
  return isNeutral(p.$variant) ? "underline" : "none";
});
export var Button = React.forwardRef(function (props, ref) {
  var className = props.className,
    disabled = props.disabled,
    onClick = props.onClick,
    _props$size = props.size,
    size = _props$size === void 0 ? 'm' : _props$size,
    _props$type = props.type,
    type = _props$type === void 0 ? 'button' : _props$type,
    _props$variant = props.variant,
    variant = _props$variant === void 0 ? 'primary' : _props$variant,
    fullWidth = props.fullWidth,
    to = props.to,
    rel = props.rel,
    target = props.target,
    children = props.children,
    id = props.id,
    color = props.color,
    as = props.as,
    loading = props.loading,
    external = props.external,
    cms = props.cms,
    fullServerRedirect = props.fullServerRedirect,
    onMouseEnter = props.onMouseEnter,
    onMouseLeave = props.onMouseLeave,
    onMouseOver = props.onMouseOver,
    _props$delayLoadingSp = props.delayLoadingSpinnerAnimation,
    delayLoadingSpinnerAnimation = _props$delayLoadingSp === void 0 ? true : _props$delayLoadingSp,
    icon = props.icon,
    _props$iconPlacement = props.iconPlacement,
    iconPlacement = _props$iconPlacement === void 0 ? 'left' : _props$iconPlacement,
    componentId = props.componentId,
    rest = _objectWithoutProperties(props, _excluded);
  var externalIsNotPresent = typeof external === 'undefined';
  var toAndDisabledAreNotPresentTogether = !(to && disabled);
  var trackContext = useContext(TrackingContext);
  var trackClick = function trackClick(e) {
    if (trackContext) trackContext.track('button', e, props);
    if (onClick) onClick(e);
  };
  var theme = useTheme();
  var colorFromTheme = color && color(theme);
  var sharedProps = _objectSpread(_objectSpread({}, rest), {}, {
    className: className,
    onClick: trackClick,
    $size: size,
    $variant: variant,
    $fullWidth: fullWidth,
    $colorFn: color,
    id: id,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onMouseOver: onMouseOver
  });
  if (colorFromTheme && (isPrimary(variant) || isSecondary(variant))) {
    assert(colorFromTheme === theme.color.cta || colorFromTheme === theme.color.negative || colorFromTheme === theme.color.sell || colorFromTheme === theme.color.functionRed || colorFromTheme === theme.color.error, 'Button: color is incorrect, use only `t => t.color.cta` or `t => t.color.negative` or `t => t.color.functionRed` or `t => t.color.error` for primary and secondary variant.');
  }
  if (cms) {
    assert(false, 'Button: the prop cms is deprecated, please use fullServerRedirect instead.', {
      level: 'warn'
    });
  }
  assert(toAndDisabledAreNotPresentTogether, "You're using `to` prop together with `disabled` prop. `Disabled` prop won't be propagated to the dom node, because <a> element can't be disabled", {
    level: 'warn'
  });
  if (to && !disabled) {
    return React.createElement(StyledLink, _extends({}, sharedProps, {
      rel: rel,
      innerRef: ref,
      external: external,
      cms: cms,
      fullServerRedirect: fullServerRedirect,
      to: to,
      as: as,
      target: target
    }), React.createElement(ButtonContent, {
      loading: loading,
      variant: variant,
      size: size,
      colorFn: color,
      delayLoadingSpinnerAnimation: delayLoadingSpinnerAnimation,
      icon: icon,
      iconPlacement: iconPlacement
    }, children));
  }
  assert(externalIsNotPresent, 'You have `external` prop on what appears to be Button component. This prop will be omitted. ', {
    level: 'warn'
  });
  return React.createElement(StyledButton, _extends({}, sharedProps, {
    disabled: disabled,
    type: type,
    as: as,
    ref: ref
  }), React.createElement(ButtonContent, {
    loading: loading,
    variant: variant,
    size: size,
    colorFn: color,
    delayLoadingSpinnerAnimation: delayLoadingSpinnerAnimation,
    icon: icon,
    iconPlacement: iconPlacement
  }, children));
});
Button.displayName = 'Button';