"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _constants = require("../../../../constants");
var _analytics = require("../../../../state/analytics");
var _types = require("../../types");
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 getResizeFrom = function getResizeFrom(size) {
  return size === _types.EmbedModalSize.Small ? _types.EmbedModalSize.Large : _types.EmbedModalSize.Small;
};
var withAnalytics = function withAnalytics(Component) {
  return function (props) {
    var fireEvent = props.fireEvent,
      extensionKey = props.extensionKey,
      id = props.id,
      onClose = props.onClose,
      onOpen = props.onOpen,
      onOpenFailed = props.onOpenFailed,
      onResize = props.onResize,
      origin = props.origin;
    var handleOnOpen = (0, _react.useCallback)(function (context) {
      fireEvent === null || fireEvent === void 0 || fireEvent('screen.embedPreviewModal.viewed', {
        origin: origin !== null && origin !== void 0 ? origin : null,
        size: context.size
      });
      (0, _analytics.succeedUfoExperience)('smart-link-rendered', id || 'NULL', {
        extensionKey: extensionKey,
        display: _constants.CardDisplay.EmbedPreview
      });

      // UFO will disregard this if authentication experience has not yet been started
      (0, _analytics.succeedUfoExperience)('smart-link-authenticated', id || 'NULL', {
        display: _constants.CardDisplay.EmbedPreview
      });
      fireEvent === null || fireEvent === void 0 || fireEvent('ui.smartLink.renderSuccess', {
        display: _constants.CardDisplay.EmbedPreview
      });
      if (onOpen) {
        onOpen(context);
      }
    }, [fireEvent, extensionKey, id, onOpen, origin]);
    var handleOnOpenFailed = (0, _react.useCallback)(function (error, errorInfo) {
      (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 === null || fireEvent === void 0 || fireEvent('ui.smartLink.renderFailed', {
        display: _constants.CardDisplay.EmbedPreview,
        error: error,
        errorInfo: errorInfo,
        id: id !== null && id !== void 0 ? id : null
      });
      if (onOpenFailed) {
        onOpenFailed(error, errorInfo);
      }
    }, [fireEvent, id, onOpenFailed]);
    var handleOnClose = (0, _react.useCallback)(function (context) {
      var _context$duration;
      fireEvent === null || fireEvent === void 0 || fireEvent('ui.modal.closed.embedPreview', {
        origin: origin !== null && origin !== void 0 ? origin : null,
        previewTime: (_context$duration = context.duration) !== null && _context$duration !== void 0 ? _context$duration : null,
        size: context.size
      });
      if (onClose) {
        onClose(context);
      }
    }, [fireEvent, onClose, origin]);
    var handleOnResize = (0, _react.useCallback)(function (context) {
      fireEvent === null || fireEvent === void 0 || fireEvent('ui.button.clicked.embedPreviewResize', {
        newSize: context.size,
        origin: origin !== null && origin !== void 0 ? origin : null,
        previousSize: getResizeFrom(context.size)
      });
      if (onResize) {
        onResize(context);
      }
    }, [fireEvent, onResize, origin]);
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, props, {
      onClose: handleOnClose,
      onOpen: handleOnOpen,
      onOpenFailed: handleOnOpenFailed,
      onResize: handleOnResize
    }));
  };
};
var _default = exports.default = withAnalytics;