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

import classNames from 'classnames';
import { useContext } from 'react';
import { isRtl } from '../../utils';
import { CLS_PREFIX } from '../../constants';
import OnDarkContext from '../OnDarkContext';
import { wrapFunction } from '../utils';
import Icon from '../Icon';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `Link` is used for navigation purposes.
 * @since 1.9.0
 */
const Link = _ref => {
  let {
    baseClassName = `${CLS_PREFIX}link`,
    className,
    onClick,
    disabled = false,
    pseudo = false,
    component,
    children,
    ...other
  } = _ref;
  const Tag = component || 'a';
  const onDark = useContext(OnDarkContext);
  const props = {
    ...other,
    onClick,
    className: classNames(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 = wrapFunction(props.onKeyDown, e => {
      if (e.key === 'Enter') {
        e.preventDefault();
        e.currentTarget.click();
      }
    });
  }
  return /*#__PURE__*/_jsxs(Tag, {
    ...props,
    children: [children, external && /*#__PURE__*/_jsx(Icon, {
      flipHorizontal: isRtl(),
      name: "arrow-diagonal-out",
      className: `${baseClassName}__external-icon`
    })]
  });
};
export default Link;