"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _reactOuiIcons = _interopRequireDefault(require("react-oui-icons"));

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

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

/**
 * @param {Object} props - Properties passed to component
 * @returns {ReactElement}
 */
var Link = function Link(_ref) {
  var _classNames;

  var ariaLabel = _ref.ariaLabel,
      children = _ref.children,
      className = _ref.className,
      href = _ref.href,
      leftIcon = _ref.leftIcon,
      onClick = _ref.onClick,
      _ref$isDisabled = _ref.isDisabled,
      isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
      _ref$isFullWidth = _ref.isFullWidth,
      isFullWidth = _ref$isFullWidth === void 0 ? false : _ref$isFullWidth,
      _ref$newWindow = _ref.newWindow,
      newWindow = _ref$newWindow === void 0 ? false : _ref$newWindow,
      rightIcon = _ref.rightIcon,
      style = _ref.style,
      testSection = _ref.testSection,
      props = _objectWithoutProperties(_ref, ["ariaLabel", "children", "className", "href", "leftIcon", "onClick", "isDisabled", "isFullWidth", "newWindow", "rightIcon", "style", "testSection"]);

  var classes = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "link", true), _defineProperty(_classNames, "link--".concat(style), style), _defineProperty(_classNames, "link--disabled", isDisabled), _defineProperty(_classNames, "pointer-events--none", isDisabled), _defineProperty(_classNames, "width--1-1", isFullWidth), _defineProperty(_classNames, "display--inline-block ", isFullWidth), _defineProperty(_classNames, "flex--inline flex-justified--center flex-align--center", leftIcon || rightIcon), _classNames), className);
  var leftIconComp = leftIcon ? _react["default"].createElement("div", {
    className: "flex flex-self--center push-half--right"
  }, _react["default"].createElement(_reactOuiIcons["default"], {
    name: leftIcon,
    size: "small"
  })) : "";
  var rightIconComp = rightIcon ? _react["default"].createElement("div", {
    className: "flex flex-self--center push-half--left"
  }, _react["default"].createElement(_reactOuiIcons["default"], {
    name: rightIcon,
    size: "small"
  })) : "";

  if (!href && !onClick) {
    return _react["default"].createElement("span", _extends({
      "data-oui-component": true,
      className: classes,
      "data-test-section": testSection,
      "data-track-id": testSection,
      disabled: isDisabled
    }, props), leftIcon && leftIconComp, children, rightIcon && rightIconComp);
  }

  return _react["default"].createElement("a", _extends({
    "aria-label": ariaLabel,
    "data-oui-component": true,
    href: href,
    className: classes,
    "data-test-section": testSection,
    "data-track-id": testSection,
    onClick: onClick
  }, newWindow ? {
    target: "_blank",
    rel: "noopener noreferrer"
  } : {}, {
    disabled: isDisabled
  }, props), leftIcon && leftIconComp, children, rightIcon && rightIconComp);
};

Link.propTypes = {
  /** Label to use for screen readers */
  ariaLabel: _propTypes["default"].string,

  /** Text/node that is linked */
  children: _propTypes["default"].node.isRequired,

  /** CSS class names. */
  className: _propTypes["default"].string,

  /** Href for the anchor element */
  href: _propTypes["default"].string,

  /** Disable link and interactivity */
  isDisabled: _propTypes["default"].bool,

  /** Whether or not this link is full width */
  isFullWidth: _propTypes["default"].bool,

  /** Icon to display on the left */
  leftIcon: _propTypes["default"].node,

  /** Open link in new window */
  newWindow: _propTypes["default"].bool,

  /** Click handler function */
  onClick: _propTypes["default"].func,

  /** Icon to display on the right */
  rightIcon: _propTypes["default"].node,

  /** Link style options */
  style: _propTypes["default"].oneOf(["default", "dark", "muted", "bad-news", "reverse"]),

  /** Hook for automated JavaScript tests */
  testSection: _propTypes["default"].string
};
var _default = Link;
exports["default"] = _default;