"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

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

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

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

var _constants = require("../../constants");

var _OnDarkContext = _interopRequireDefault(require("../OnDarkContext"));

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

var _Icon = _interopRequireDefault(require("../Icon"));

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/**
 * FIXME: With the following code all @ts-ignore comments can be removed, but react-docgen cannot parse it.
 * const Link = <P extends any>({ ... }: LinkProps<P>) => {
 */

/**
 * `Link` is used for navigation purposes.
 * @since 1.9.0
 */
const Link = ({
  baseClassName = "".concat(_constants.CLS_PREFIX, "link"),
  className,
  onClick,
  disabled = false,
  pseudo = false,
  component,
  children,
  onKeyDown,
  ...other
}) => {
  const Component = component ? component : 'a';
  const onDark = (0, _react.useContext)(_OnDarkContext.default);
  const props = { ...other,
    onClick,
    className: (0, _classnames.default)(baseClassName, className, {
      ["".concat(baseClassName, "--disabled")]: disabled,
      ["".concat(baseClassName, "--pseudo")]: pseudo,
      ["".concat(baseClassName, "--on-dark")]: onDark
    })
  };
  const {
    target
  } = other;
  const external = !pseudo && target === '_blank';

  if (external && !props.rel) {
    props.rel = 'noopener noreferrer';
  }

  if (disabled) {
    props.onClick = e => e.preventDefault();

    props.tabIndex = -1;
  } // accessibility


  if (onClick) {
    props.tabIndex = props.tabIndex || 0;

    props.onKeyDown = e => {
      if (e.key === 'Enter') {
        e.preventDefault();
        e.currentTarget.click();
      }

      (0, _utils2.safeInvoke)(onKeyDown, e);
    };
  }

  return (
    /*#__PURE__*/
    // @ts-ignore
    _react.default.createElement(Component, props, children, external && /*#__PURE__*/_react.default.createElement(_Icon.default, {
      flipHorizontal: (0, _utils.isRtl)(),
      name: "arrow-diagonal-out",
      className: "".concat(baseClassName, "__external-icon")
    }))
  );
};

var _default = Link;
exports.default = _default;