import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "disabled", "element", "inherit", "inverse", "selected", "onMouseUp", "onMouseLeave"];
import uiUtilities from '@teamleader/ui-utilities';
import cx from 'classnames';
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
import Box from '../box';
import theme from './theme.css';
var Link = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _cx;

  var children = _ref.children,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      _ref$element = _ref.element,
      element = _ref$element === void 0 ? 'a' : _ref$element,
      _ref$inherit = _ref.inherit,
      inherit = _ref$inherit === void 0 ? true : _ref$inherit,
      _ref$inverse = _ref.inverse,
      inverse = _ref$inverse === void 0 ? false : _ref$inverse,
      selected = _ref.selected,
      onMouseUp = _ref.onMouseUp,
      onMouseLeave = _ref.onMouseLeave,
      others = _objectWithoutProperties(_ref, _excluded);

  var linkRef = useRef(null);
  useImperativeHandle(ref, function () {
    return linkRef.current;
  });

  var blur = function blur() {
    var currentLinkRef = linkRef.current;

    if (currentLinkRef !== null && currentLinkRef !== void 0 && currentLinkRef.blur) {
      currentLinkRef.blur();
    }
  };

  var handleMouseUp = function handleMouseUp(event) {
    blur();
    onMouseUp && onMouseUp(event);
  };

  var handleMouseLeave = function handleMouseLeave(event) {
    blur();
    onMouseLeave && onMouseLeave(event);
  };

  var classNames = cx(uiUtilities['reset-font-smoothing'], theme['link'], (_cx = {}, _defineProperty(_cx, theme['is-disabled'], disabled), _defineProperty(_cx, theme['is-inherit'], inherit), _defineProperty(_cx, theme['is-inverse'], inverse), _defineProperty(_cx, theme['is-selected'], selected), _cx), className);
  return /*#__PURE__*/React.createElement(Box, _extends({
    element: element,
    ref: linkRef,
    className: classNames,
    "data-teamleader-ui": "link",
    onMouseUp: handleMouseUp,
    onMouseLeave: handleMouseLeave
  }, others), children);
});
Link.displayName = 'Link';
export default Link;