"use strict";

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }

// Copyright 1999-2022. 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 = `${_constants.CLS_PREFIX}link`,
  className,
  onClick,
  disabled = false,
  pseudo = false,
  component,
  children,
  ...other
}) => {
  const Component = component ? component : 'a';
  const onDark = (0, _react.useContext)(_OnDarkContext.default);
  const props = { ...other,
    onClick,
    className: (0, _classnames.default)(baseClassName, className, {
      [`${baseClassName}--disabled`]: disabled,
      [`${baseClassName}--pseudo`]: pseudo,
      [`${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 = (0, _utils2.wrapFunction)(props.onKeyDown, e => {
      if (e.key === 'Enter') {
        e.preventDefault();
        e.currentTarget.click();
      }
    });
  }

  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: `${baseClassName}__external-icon`
    }))
  );
};

var _default = Link;
exports.default = _default;