"use strict";

exports.__esModule = true;
exports.default = void 0;
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _buttonIcons = require("../../util/button-icons");
var _icon = _interopRequireDefault(require("../icon"));
var _link = _interopRequireDefault(require("../link"));
var _tooltip = _interopRequireDefault(require("../tooltip"));
var _style = _interopRequireDefault(require("./style.css"));
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); } // eslint-disable-next-line css-modules/no-unused-class
const getButtonContent = (icon, faIcon) => {
  const Icon = (0, _getOr2.default)(null, icon, _buttonIcons.ICONS);
  if (!Icon && !faIcon) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.buttonContent
    });
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.buttonContent
  }, faIcon ? /*#__PURE__*/_react.default.createElement(_icon.default, {
    iconName: faIcon
  }) : /*#__PURE__*/_react.default.createElement(Icon, {
    className: _style.default.icon
  }));
};
const getSizeStyle = size => {
  switch (size) {
    case 'small':
      return _style.default.small;
    case 'responsive':
      return _style.default.responsive;
    default:
      return _style.default.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] = (0, _react.useState)(false);
  const timer = (0, _react.useRef)();
  const contentView = getButtonContent(icon, faIcon);
  const styleButton = (0, _classnames.default)(getSizeStyle(size), link && _style.default.link, disabled && _style.default.disabled, className);
  const handleOnClick = (0, _react.useCallback)(event => onClick(event), [onClick]);
  const handleMouseOver = (0, _react.useCallback)(() => {
    timer.current && clearTimeout(timer.current);
    timer.current = setTimeout(() => {
      setToolTipIsVisible(true);
      timer.current = undefined;
    }, 300);
  }, [timer, setToolTipIsVisible]);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    timer.current && clearTimeout(timer.current);
    setToolTipIsVisible(false);
  }, [timer, setToolTipIsVisible]);
  const TooltipContent = (0, _react.useCallback)(() => /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.tooltipContentWrapper
  }, ariaLabel), [ariaLabel]);
  const Button = (0, _react.useCallback)(() => link ? /*#__PURE__*/_react.default.createElement(_link.default, _extends({}, link, ariaLabel ? {
    'data-for': dataName,
    'data-tip': toolTipIsVisible
  } : {}, {
    className: styleButton,
    "data-name": dataName,
    "aria-label": ariaLabel,
    onMouseEnter: handleMouseOver,
    onMouseLeave: handleMouseLeave
  }), contentView) : /*#__PURE__*/_react.default.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.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(Button, null), ariaLabel ? /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    fontSize: 12,
    anchorId: dataName,
    toolTipIsVisible: toolTipIsVisible,
    placement: tooltipPlacement,
    TooltipContent: TooltipContent,
    closeToolTipInformationTextAriaLabel: ariaLabel
  }) : null);
};
ButtonLinkIcon.propTypes = process.env.NODE_ENV !== "production" ? {
  size: _propTypes.default.oneOf(['default', 'small', 'responsive']),
  'aria-label': _propTypes.default.string,
  'data-name': _propTypes.default.string,
  icon: _propTypes.default.oneOf((0, _keys2.default)(_buttonIcons.ICONS)),
  faIcon: _propTypes.default.string,
  onClick: _propTypes.default.func,
  link: _propTypes.default.shape({
    href: _propTypes.default.string,
    download: _propTypes.default.bool,
    target: _propTypes.default.oneOf(['_self', '_blank', '_parent', '_top'])
  }),
  disabled: _propTypes.default.bool,
  className: _propTypes.default.string,
  tooltipPlacement: _propTypes.default.oneOf(['left', 'right', 'top', 'bottom'])
} : {};
var _default = exports.default = ButtonLinkIcon;
//# sourceMappingURL=index.js.map