"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Link = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _utils = require("../../common/utils");
var _NormalizedElements = _interopRequireDefault(require("../../common/NormalizedElements"));
var _tracking = _interopRequireDefault(require("../../common/tracking"));
var _Links = require("../../common/Links");
var _excluded = ["to", "children", "disabled", "display", "className", "onClick", "external", "cms", "fullServerRedirect", "as", "color", "underlined", "onMouseEnter", "onMouseLeave", "onMouseOver", "onFocus", "componentId"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 _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 getEnabledColor = function getEnabledColor(color, theme) {
  if (color === 'black') {
    return theme.color.text;
  }
  if (color === 'inherit') {
    return color;
  }
  return theme.color.link;
};
var getSharedStyle = function getSharedStyle(props) {
  var theme = props.theme,
    disabled = props.disabled,
    _props$$display = props.$display,
    $display = _props$$display === void 0 ? 'inline' : _props$$display,
    $color = props.$color;
  return "\n    display: ".concat($display, ";\n    padding: 0;\n    color: ").concat(disabled ? theme.color.disabledText : getEnabledColor($color, theme), ";\n\n    &:hover {\n      text-decoration: underline;\n    }\n  ");
};
var CleanLink = function CleanLink(props) {
  var RawLink = (0, _Links.useLink)();
  return _react.default.createElement(RawLink, props);
};
var StyledLink = (0, _styledComponents.default)(CleanLink).withConfig({
  displayName: "Link__StyledLink",
  componentId: "sc-apj04t-0"
})(["", " text-decoration:", ";"], function (p) {
  return getSharedStyle(p);
}, function (p) {
  return p.$underlined ? "underline" : "none";
});
var StyledButton = (0, _styledComponents.default)(_NormalizedElements.default.Button).withConfig({
  displayName: "Link__StyledButton",
  componentId: "sc-apj04t-1"
})(["", " border:none;background:transparent;overflow:visible;-webkit-appearance:none !important;", ";cursor:", ";font-weight:inherit;"], function (p) {
  return getSharedStyle(p);
}, function (p) {
  return p.$underlined ? "text-decoration: underline;" : "";
}, function (p) {
  return p.disabled ? 'not-allowed' : 'pointer';
});
var Link = exports.Link = _react.default.forwardRef(function (props, ref) {
  var to = props.to,
    children = props.children,
    disabled = props.disabled,
    display = props.display,
    className = props.className,
    onClick = props.onClick,
    external = props.external,
    cms = props.cms,
    fullServerRedirect = props.fullServerRedirect,
    as = props.as,
    color = props.color,
    underlined = props.underlined,
    onMouseEnter = props.onMouseEnter,
    onMouseLeave = props.onMouseLeave,
    onMouseOver = props.onMouseOver,
    onFocus = props.onFocus,
    componentId = props.componentId,
    rest = _objectWithoutProperties(props, _excluded);
  var _useContext = (0, _react.useContext)(_tracking.default),
    track = _useContext.track;
  var trackClick = function trackClick(e) {
    if (track) track('link', e, props);
    if (onClick) onClick(e);
  };
  if ((0, _utils.isUndefined)(to) || disabled) {
    return _react.default.createElement(StyledButton, _extends({
      type: "button",
      ref: ref,
      className: className,
      onClick: trackClick,
      disabled: disabled,
      as: as,
      $color: color,
      $display: display,
      $underlined: underlined
    }, rest), children);
  }
  if (cms) {
    (0, _utils.assert)(false, 'Link: the prop cms is deprecated, please use fullServerRedirect instead.', {
      level: 'warn'
    });
  }
  return _react.default.createElement(StyledLink, _extends({
    innerRef: ref,
    className: className,
    onClick: trackClick,
    to: to,
    external: external,
    cms: cms,
    fullServerRedirect: fullServerRedirect,
    as: as,
    $color: color,
    $display: display,
    $underlined: underlined,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onMouseOver: onMouseOver,
    onFocus: onFocus
  }, rest), children);
});