"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _constants = require("../../../../../constants");
var _utils = require("../../../../../utils");
var _HoverCard = require("../../../../HoverCard");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 = (0, _react.useState)(true),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    canOpen = _useState2[0],
    setCanOpen = _useState2[1];
  var mouseStopTimer = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    return function () {
      if (mouseStopTimer.current) {
        clearTimeout(mouseStopTimer.current);
      }
    };
  }, []);
  var onMouseLeave = (0, _react.useCallback)(function () {
    if (mouseStopTimer.current) {
      clearTimeout(mouseStopTimer.current);
    }
  }, []);
  var onMouseMove = (0, _react.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(_constants.ElementName.Title, "\"])"));
    mouseStopTimer.current = setTimeout(function () {
      if (canOpen !== canOpenOnElement) {
        setCanOpen(Boolean(canOpenOnElement));
      }
    }, delay);
  }, [isAuthTooltip, isHoverPreview, canOpen, delay]);
  return /*#__PURE__*/_react.default.createElement(_HoverCard.HoverCard, {
    allowEventPropagation: true,
    canOpen: canOpen,
    closeOnChildClick: true,
    actionOptions: actionOptions,
    url: url,
    hoverPreviewOptions: hoverPreviewOptions
  }, /*#__PURE__*/_react.default.createElement("span", {
    onBlur: _utils.noop,
    onMouseLeave: onMouseLeave,
    onMouseMove: onMouseMove,
    "data-testid": "".concat(testId, "-hover-card-wrapper"),
    role: 'presentation'
  }, children));
};
var _default = exports.default = HoverCardControl;