import _keys from "lodash/fp/keys";
import _getOr from "lodash/fp/getOr";
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, { useState, useCallback, useRef } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { ICONS } from '../../util/button-icons';
import FaIcon from '../icon';
import Link from '../link';
import ToolTip from '../tooltip';
// eslint-disable-next-line css-modules/no-unused-class
import style from './style.css';
const getButtonContent = (icon, faIcon) => {
  const Icon = _getOr(null, icon, ICONS);
  if (!Icon && !faIcon) {
    return /*#__PURE__*/React.createElement("div", {
      className: style.buttonContent
    });
  }
  return /*#__PURE__*/React.createElement("div", {
    className: style.buttonContent
  }, faIcon ? /*#__PURE__*/React.createElement(FaIcon, {
    iconName: faIcon
  }) : /*#__PURE__*/React.createElement(Icon, {
    className: style.icon
  }));
};
const getSizeStyle = size => {
  switch (size) {
    case 'small':
      return style.small;
    case 'responsive':
      return style.responsive;
    default:
      return style.defaultStyle;
  }
};
const ButtonLinkIcon = props => {
  const {
    size = 'default',
    disabled,
    icon,
    faIcon,
    'data-name': dataName = 'button-icon',
    'aria-label': ariaLabel,
    link,
    onClick,
    className,
    tooltipPlacement = 'left'
  } = props;
  const [toolTipIsVisible, setToolTipIsVisible] = useState(false);
  const timer = useRef();
  const contentView = getButtonContent(icon, faIcon);
  const styleButton = classnames(getSizeStyle(size), link && style.link, disabled && style.disabled, className);
  const handleOnClick = useCallback(event => onClick(event), [onClick]);
  const handleMouseOver = useCallback(() => {
    timer.current && clearTimeout(timer.current);
    timer.current = setTimeout(() => {
      setToolTipIsVisible(true);
      timer.current = undefined;
    }, 300);
  }, [timer, setToolTipIsVisible]);
  const handleMouseLeave = useCallback(() => {
    timer.current && clearTimeout(timer.current);
    setToolTipIsVisible(false);
  }, [timer, setToolTipIsVisible]);
  const TooltipContent = useCallback(() => /*#__PURE__*/React.createElement("span", {
    className: style.tooltipContentWrapper
  }, ariaLabel), [ariaLabel]);
  const Button = useCallback(() => link ? /*#__PURE__*/React.createElement(Link, _extends({}, link, ariaLabel ? {
    'data-for': dataName,
    'data-tip': toolTipIsVisible
  } : {}, {
    className: styleButton,
    "data-name": dataName,
    "aria-label": ariaLabel,
    onMouseEnter: handleMouseOver,
    onMouseLeave: handleMouseLeave
  }), contentView) : /*#__PURE__*/React.createElement("button", _extends({}, ariaLabel ? {
    'data-for': dataName,
    'data-tip': toolTipIsVisible
  } : {}, {
    type: "button",
    "aria-label": ariaLabel,
    "data-name": dataName,
    "data-testid": `button-${dataName}-${className}`,
    className: styleButton,
    onClick: handleOnClick,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver,
    disabled: disabled
  }), contentView), [link, ariaLabel, className, contentView, dataName, disabled, handleMouseLeave, handleMouseOver, handleOnClick, styleButton, toolTipIsVisible]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, null), ariaLabel ? /*#__PURE__*/React.createElement(ToolTip, {
    fontSize: 12,
    anchorId: dataName,
    toolTipIsVisible: toolTipIsVisible,
    placement: tooltipPlacement,
    TooltipContent: TooltipContent,
    closeToolTipInformationTextAriaLabel: ariaLabel
  }) : null);
};
ButtonLinkIcon.propTypes = process.env.NODE_ENV !== "production" ? {
  size: PropTypes.oneOf(['default', 'small', 'responsive']),
  'aria-label': PropTypes.string,
  'data-name': PropTypes.string,
  icon: PropTypes.oneOf(_keys(ICONS)),
  faIcon: PropTypes.string,
  onClick: PropTypes.func,
  link: PropTypes.shape({
    href: PropTypes.string,
    download: PropTypes.bool,
    target: PropTypes.oneOf(['_self', '_blank', '_parent', '_top'])
  }),
  disabled: PropTypes.bool,
  className: PropTypes.string,
  tooltipPlacement: PropTypes.oneOf(['left', 'right', 'top', 'bottom'])
} : {};
export default ButtonLinkIcon;
//# sourceMappingURL=index.js.map