import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { ElementName } from '../../../../../constants';
import { HoverCard } from '../../../../HoverCard';
var FLEXIBLE_HOVER_CARD_CAN_OPEN_DELAY = 100;
var HoverCardControl = function HoverCardControl(_ref) {
  var children = _ref.children,
    isHoverPreview = _ref.isHoverPreview,
    isAuthTooltip = _ref.isAuthTooltip,
    actionOptions = _ref.actionOptions,
    testId = _ref.testId,
    url = _ref.url,
    _ref$delay = _ref.delay,
    delay = _ref$delay === void 0 ? FLEXIBLE_HOVER_CARD_CAN_OPEN_DELAY : _ref$delay,
    hoverPreviewOptions = _ref.hoverPreviewOptions;
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    canOpen = _useState2[0],
    setCanOpen = _useState2[1];
  var mouseStopTimer = useRef();
  useEffect(function () {
    return function () {
      if (mouseStopTimer.current) {
        clearTimeout(mouseStopTimer.current);
      }
    };
  }, []);
  var onMouseLeave = useCallback(function () {
    if (mouseStopTimer.current) {
      clearTimeout(mouseStopTimer.current);
    }
  }, []);
  var onMouseMove = useCallback(function (e) {
    var _e$target$closest;
    if (mouseStopTimer.current) {
      clearTimeout(mouseStopTimer.current);
    }

    // Never show hover card on action or when action dropdown opens.
    // The code below can be simplified by using :is() and :has()
    // but the pseudo-class isn't support by Firefox yet.
    var action =
    // Any action button group (title/footer block)
    e.target.closest('.actions-button-group') || ( // When action dropdown list is opened on action button group or lozenge action
    (_e$target$closest = e.target.closest('[data-smart-link-container]')) === null || _e$target$closest === void 0 ? void 0 : _e$target$closest.querySelector('[data-action-open="true"]'));
    var canOpenOnElement = isAuthTooltip && !action ||
    // EDM-7060: For hover preview, also hide hover card on all elements
    // except title element (link title)
    isHoverPreview && !action && !e.target.closest("[data-smart-element]:not([data-smart-element=\"".concat(ElementName.Title, "\"])"));
    mouseStopTimer.current = setTimeout(function () {
      if (canOpen !== canOpenOnElement) {
        setCanOpen(Boolean(canOpenOnElement));
      }
    }, delay);
  }, [isAuthTooltip, isHoverPreview, canOpen, delay]);
  return /*#__PURE__*/React.createElement(HoverCard, {
    allowEventPropagation: true,
    canOpen: canOpen,
    closeOnChildClick: true,
    actionOptions: actionOptions,
    url: url,
    hoverPreviewOptions: hoverPreviewOptions
  }, /*#__PURE__*/React.createElement("span", {
    // eslint-disable-next-line @atlassian/a11y/mouse-events-have-key-events
    onMouseLeave: onMouseLeave,
    onMouseMove: onMouseMove,
    "data-testid": "".concat(testId, "-hover-card-wrapper"),
    role: 'presentation'
  }, children));
};
export default HoverCardControl;