"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LINK_WEIGHT = exports.LINK_TRANSFORM = exports.LINK_SIZE = exports.LINK_ALIGN = void 0;
Object.defineProperty(exports, "TEXT_COLOR", {
  enumerable: true,
  get: function get() {
    return _textConsts.TEXT_COLOR;
  }
});
exports.default = void 0;

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

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

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

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

var _textConsts = require("./textConsts");

var _responsiveProps = require("../utils/responsive-props");

var _excluded = ["children", "as", "href", "color", "underlined", "unstyled", "emphasised", "disabled", "weight", "className", "inherited", "size", "aria-label", "target", "onClick", "newTabLabel", "hideNewTabIndicator"];

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

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

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 anchorRelatedProps = ['download', 'hreflang', 'ping', 'referrerpolicy', 'rel'];
var LINK_SIZE = _textConsts.TEXT_SIZE;
exports.LINK_SIZE = LINK_SIZE;
var LINK_WEIGHT = _textConsts.TEXT_WEIGHT;
exports.LINK_WEIGHT = LINK_WEIGHT;
var LINK_TRANSFORM = _textConsts.TEXT_TRANSFORM;
exports.LINK_TRANSFORM = LINK_TRANSFORM;
var LINK_ALIGN = _textConsts.TEXT_ALIGN;
exports.LINK_ALIGN = LINK_ALIGN;

var Link = function Link(props) {
  var _ref;

  var children = props.children,
      _props$as = props.as,
      as = _props$as === void 0 ? 'a' : _props$as,
      href = props.href,
      color = props.color,
      _props$underlined = props.underlined,
      underlined = _props$underlined === void 0 ? false : _props$underlined,
      _props$unstyled = props.unstyled,
      unstyled = _props$unstyled === void 0 ? false : _props$unstyled,
      _props$emphasised = props.emphasised,
      emphasised = _props$emphasised === void 0 ? true : _props$emphasised,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      weight = props.weight,
      className = props.className,
      _props$inherited = props.inherited,
      inherited = _props$inherited === void 0 ? false : _props$inherited,
      size = props.size,
      ariaLabel = props['aria-label'],
      target = props.target,
      onClick = props.onClick,
      _props$newTabLabel = props.newTabLabel,
      newTabLabel = _props$newTabLabel === void 0 ? '(opens in a new tab)' : _props$newTabLabel,
      _props$hideNewTabIndi = props.hideNewTabIndicator,
      hideNewTabIndicator = _props$hideNewTabIndi === void 0 ? false : _props$hideNewTabIndi,
      additionalProps = _objectWithoutProperties(props, _excluded);

  var _React$useRef = React.useRef((0, _utils.generateId)()),
      labelId = _React$useRef.current;

  var textSize;

  if (_typeof(size) === 'object') {
    if (Array.isArray(size)) {
      textSize = size.map(function (sizeItem) {
        return sizeItem;
      });
    } else {
      textSize = {
        sm: size.sm,
        md: size.md,
        lg: size.lg,
        xl: size.xl
      };
    }
  } else if (size) {
    textSize = size;
  }

  if (_utils.__DEV__) {
    (0, _utils.invariant)(!(as === 'a' && !disabled && (href === null || href === undefined)), 'An anchor element without a href will be accessible only for users with a pointing device.');
    (0, _utils.invariant)(!(as === 'button' && (href || Object.keys(additionalProps).some(function (p) {
      return anchorRelatedProps.includes(p);
    }))), // $FlowFixMe
    "An anchor-related prop is not working for as=\"button\": ".concat(Object.keys(additionalProps)));
  }

  var linkClass = _classnames.default.apply(void 0, [(_ref = {}, _defineProperty(_ref, "sg-text--inherited", inherited), _defineProperty(_ref, 'sg-text--link', !underlined && !unstyled), _defineProperty(_ref, 'sg-text--link-underlined', underlined && !unstyled), _defineProperty(_ref, 'sg-text--link-unstyled', !underlined && unstyled), _defineProperty(_ref, 'sg-text--bold', emphasised && !inherited), _defineProperty(_ref, 'sg-text--link-disabled', disabled), _defineProperty(_ref, "sg-text--".concat(String(color)), color && !unstyled), _defineProperty(_ref, 'sg-text--link-label', as === 'button'), _ref)].concat(_toConsumableArray((0, _responsiveProps.generateResponsiveClassNames)(function (weight) {
    return "sg-text--".concat(weight);
  }, weight)), [className])); // suppressHydrationWarning is used until 'useId' hook is available


  if (as === 'button') {
    return /*#__PURE__*/React.createElement(_Text.default, _extends({}, additionalProps, {
      as: "label",
      className: linkClass,
      size: textSize
    }), /*#__PURE__*/React.createElement("span", {
      id: labelId,
      "aria-label": ariaLabel,
      "aria-hidden": true,
      suppressHydrationWarning: true
    }, children), /*#__PURE__*/React.createElement("button", {
      className: "sg-visually-hidden sg-text--link-button",
      onClick: onClick,
      disabled: disabled,
      type: "button",
      "aria-labelledby": labelId,
      suppressHydrationWarning: true
    }));
  }

  var linkType = disabled ? 'span' : 'a';

  var onLinkClick = function onLinkClick(e) {
    if (!disabled && onClick) {
      return onClick(e);
    }
  };

  return /*#__PURE__*/React.createElement(_Text.default, _extends({}, additionalProps, {
    as: linkType,
    href: href || '',
    className: linkClass,
    size: textSize,
    onClick: onLinkClick,
    "aria-label": ariaLabel,
    target: target
  }), children, target === '_blank' && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
    "aria-hidden": true,
    hidden: hideNewTabIndicator
  }, "\u2B08"), /*#__PURE__*/React.createElement("span", {
    className: "sg-visually-hidden"
  }, newTabLabel)));
};

var _default = Link;
exports.default = _default;