import _noop from "lodash/fp/noop";
import _identity from "lodash/fp/identity";
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); }
import React, { useCallback, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import Provider from '../provider';
import pushToHistory from '../../util/navigation';
const Link = (props, legacyContext) => {
  const [hovered, setHovered] = useState(false);
  const {
    history: {
      createHref = _identity
    } = {}
  } = legacyContext;
  const {
    hoverColor,
    hoverBackgroundColor,
    'data-name': dataName = 'link',
    useButtonTag = false,
    href,
    onClick = _noop,
    style: customStyle,
    children,
    onMouseEnter = _noop,
    onMouseLeave = _noop,
    download,
    disabled,
    ...restProps
  } = props;
  const navigate = useMemo(() => pushToHistory(legacyContext)({
    href
  }), [href, legacyContext]);
  const handleMouseEnter = useCallback(() => {
    setHovered(true);
    onMouseEnter();
  }, [onMouseEnter]);
  const handleMouseLeave = useCallback(() => {
    setHovered(false);
    onMouseLeave();
  }, [onMouseLeave]);
  const handleOnClick = 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.createElement("button", _extends({}, restProps, {
    "data-name": dataName,
    type: "button",
    onClick: disabled ? _noop : handleOnClick,
    style: style,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave
  }), children) : /*#__PURE__*/React.createElement("a", _extends({}, restProps, {
    "data-name": dataName,
    href: disabled || !href ? undefined : createHref(href),
    onClick: disabled ? _noop : handleOnClick,
    style: style,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave
  }), children);
};
Link.propTypes = process.env.NODE_ENV !== "production" ? {
  children: PropTypes.node,
  className: PropTypes.string,
  href: PropTypes.string,
  'data-name': PropTypes.string,
  'aria-label': PropTypes.string,
  title: PropTypes.string,
  target: PropTypes.oneOf(['_self', '_blank', '_parent', '_top']),
  hoverColor: PropTypes.string,
  hoverBackgroundColor: PropTypes.string,
  download: PropTypes.bool,
  onClick: PropTypes.func,
  onMouseEnter: PropTypes.func,
  onMouseLeave: PropTypes.func,
  useButtonTag: PropTypes.bool,
  style: PropTypes.shape({}),
  disabled: PropTypes.bool
} : {};
Link.contextTypes = {
  skin: Provider.childContextTypes.skin,
  history: Provider.childContextTypes.history
};
export default Link;
//# sourceMappingURL=index.js.map