"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StandaloneHoverCard = exports.HoverCard = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _reactErrorBoundary = require("react-error-boundary");
var _reactMagneticDi = require("react-magnetic-di");
var _analyticsNext = require("@atlaskit/analytics-next");
var _useAnalyticsEvents2 = require("../../common/analytics/generated/use-analytics-events");
var _constants = require("../../constants");
var _analytics = require("../../state/analytics");
var _modal = require("../../state/modal");
var _SmartLinkAnalyticsContext = require("../../utils/analytics/SmartLinkAnalyticsContext");
var _HoverCardComponent = require("./components/HoverCardComponent");
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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var HoverCardWithErrorBoundary = function HoverCardWithErrorBoundary(props) {
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var url = props.url,
    id = props.id,
    children = props.children;
  var analyticsContext = (0, _SmartLinkAnalyticsContext.useSmartLinkAnalyticsContext)({
    display: _constants.CardDisplay.HoverCardPreview,
    id: id,
    source: _HoverCardComponent.HOVER_CARD_SOURCE,
    url: url
  });
  var onError = (0, _react.useCallback)(function (error, info) {
    (0, _analytics.startUfoExperience)('smart-link-rendered', id || 'NULL');
    (0, _analytics.failUfoExperience)('smart-link-rendered', id || 'NULL');
    (0, _analytics.failUfoExperience)('smart-link-authenticated', id || 'NULL');
    fireEvent('ui.smartLink.renderFailed', _objectSpread(_objectSpread({}, analyticsContext === null || analyticsContext === void 0 ? void 0 : analyticsContext.attributes), {}, {
      display: _constants.CardDisplay.HoverCardPreview,
      id: id !== null && id !== void 0 ? id : null,
      error: error,
      errorInfo: info
    }));
  }, [analyticsContext, id, fireEvent]);
  return /*#__PURE__*/_react.default.createElement(_reactErrorBoundary.ErrorBoundary, {
    fallback: children,
    onError: onError
  }, /*#__PURE__*/_react.default.createElement(_modal.SmartLinkModalProvider, null, /*#__PURE__*/_react.default.createElement(_HoverCardComponent.HoverCardComponent, props, children)));
};
var HoverCardWithoutAnalyticsContext = (0, _analyticsNext.withAnalyticsEvents)()(HoverCardWithErrorBoundary);

/**
 * A hover preview component using within smart links,
 * e.g. inline card's hover preview and auth tooltip, flexible card's hover preview.
 *
 * This component contains additional props that smart-card internal components
 * use to configure hover preview behaviour.
 */
var HoverCard = exports.HoverCard = function HoverCard(props) {
  return /*#__PURE__*/_react.default.createElement(HoverCardWithoutAnalyticsContext, props);
};

/**
 * A standalone hover preview component
 */
var StandaloneHoverCard = exports.StandaloneHoverCard = function StandaloneHoverCard(props) {
  return /*#__PURE__*/_react.default.createElement(HoverCard, props);
};