"use strict";

exports.__esModule = true;
exports.default = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _identity2 = _interopRequireDefault(require("lodash/fp/identity"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _provider = _interopRequireDefault(require("../provider"));
var _navigation = _interopRequireDefault(require("../../util/navigation"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const Link = (props, legacyContext) => {
  const [hovered, setHovered] = (0, _react.useState)(false);
  const {
    history: {
      createHref = _identity2.default
    } = {}
  } = legacyContext;
  const {
    hoverColor,
    hoverBackgroundColor,
    'data-name': dataName = 'link',
    useButtonTag = false,
    href,
    onClick = _noop2.default,
    style: customStyle,
    children,
    onMouseEnter = _noop2.default,
    onMouseLeave = _noop2.default,
    download,
    disabled,
    ...restProps
  } = props;
  const navigate = (0, _react.useMemo)(() => (0, _navigation.default)(legacyContext)({
    href
  }), [href, legacyContext]);
  const handleMouseEnter = (0, _react.useCallback)(() => {
    setHovered(true);
    onMouseEnter();
  }, [onMouseEnter]);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    setHovered(false);
    onMouseLeave();
  }, [onMouseLeave]);
  const handleOnClick = (0, _react.useCallback)(e => {
    onClick(e);
    if (!download) navigate(e);
  }, [download, navigate, onClick]);
  const style = {
    ...customStyle,
    ...(hovered && hoverBackgroundColor ? {
      backgroundColor: hoverBackgroundColor
    } : undefined),
    ...(hovered && hoverColor ? {
      color: hoverColor
    } : undefined),
    ...(href || onClick ? null : {
      pointerEvents: 'none'
    })
  };
  return useButtonTag ? /*#__PURE__*/_react.default.createElement("button", _extends({}, restProps, {
    "data-name": dataName,
    type: "button",
    onClick: disabled ? _noop2.default : handleOnClick,
    style: style,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave
  }), children) : /*#__PURE__*/_react.default.createElement("a", _extends({}, restProps, {
    "data-name": dataName,
    href: disabled || !href ? undefined : createHref(href),
    onClick: disabled ? _noop2.default : handleOnClick,
    style: style,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave
  }), children);
};
Link.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes.default.node,
  className: _propTypes.default.string,
  href: _propTypes.default.string,
  'data-name': _propTypes.default.string,
  'aria-label': _propTypes.default.string,
  title: _propTypes.default.string,
  target: _propTypes.default.oneOf(['_self', '_blank', '_parent', '_top']),
  hoverColor: _propTypes.default.string,
  hoverBackgroundColor: _propTypes.default.string,
  download: _propTypes.default.bool,
  onClick: _propTypes.default.func,
  onMouseEnter: _propTypes.default.func,
  onMouseLeave: _propTypes.default.func,
  useButtonTag: _propTypes.default.bool,
  style: _propTypes.default.shape({}),
  disabled: _propTypes.default.bool
} : {};
Link.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  history: _provider.default.childContextTypes.history
};
var _default = exports.default = Link;
//# sourceMappingURL=index.js.map