"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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = require("react");
var _uuid = _interopRequireDefault(require("uuid"));
var _useAnalyticsEvents2 = require("../../../common/analytics/generated/use-analytics-events");
var measure = _interopRequireWildcard(require("../../../utils/performance"));
var _analytics = require("../../analytics");
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); }
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead

var ACTION_EXPERIENCE_NAME = 'smart-link-action-invocation';

/**
 * Invoke client action such as preview, download and open link
 */
var useInvokeClientAction = function useInvokeClientAction(_ref) {
  var fireEventProp = _ref.fireEvent;
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    defaultFireEvent = _useAnalyticsEvents.fireEvent;
  var fireEvent = fireEventProp !== null && fireEventProp !== void 0 ? fireEventProp : defaultFireEvent;
  return (0, _react.useCallback)( /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref2) {
      var actionSubjectId, actionType, actionFn, _ref2$definitionId, definitionId, extensionKey, display, id, _ref2$resourceType, resourceType, experienceId, markName, _measure$getMeasure$d, _measure$getMeasure, result, _measure$getMeasure$d2, _measure$getMeasure2, reason;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            actionSubjectId = _ref2.actionSubjectId, actionType = _ref2.actionType, actionFn = _ref2.actionFn, _ref2$definitionId = _ref2.definitionId, definitionId = _ref2$definitionId === void 0 ? null : _ref2$definitionId, extensionKey = _ref2.extensionKey, display = _ref2.display, id = _ref2.id, _ref2$resourceType = _ref2.resourceType, resourceType = _ref2$resourceType === void 0 ? null : _ref2$resourceType;
            // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
            experienceId = (0, _uuid.default)(); // Begin performance instrumentation.
            markName = "".concat(experienceId, "-").concat(actionType);
            measure.mark(markName, 'pending');
            _context.prev = 4;
            // Begin UFO experience
            (0, _analytics.startUfoExperience)(ACTION_EXPERIENCE_NAME, experienceId, {
              actionType: actionType,
              display: display,
              extensionKey: extensionKey,
              invokeType: 'client'
            });

            // Begin analytics instrumentation.
            if (actionSubjectId !== undefined) {
              fireEvent("ui.button.clicked.".concat(actionSubjectId), {
                actionType: actionType !== null && actionType !== void 0 ? actionType : null,
                definitionId: definitionId,
                display: display !== null && display !== void 0 ? display : null,
                id: id !== null && id !== void 0 ? id : experienceId,
                resourceType: resourceType
              });
            }
            // Invoke action
            _context.next = 9;
            return actionFn();
          case 9:
            result = _context.sent;
            measure.mark(markName, 'resolved');
            (0, _analytics.succeedUfoExperience)(ACTION_EXPERIENCE_NAME, experienceId);
            fireEvent('operational.smartLinkAction.resolved', {
              actionType: actionType !== null && actionType !== void 0 ? actionType : null,
              definitionId: definitionId,
              display: display !== null && display !== void 0 ? display : null,
              duration: (_measure$getMeasure$d = (_measure$getMeasure = measure.getMeasure(markName, 'resolved')) === null || _measure$getMeasure === void 0 ? void 0 : _measure$getMeasure.duration) !== null && _measure$getMeasure$d !== void 0 ? _measure$getMeasure$d : null,
              id: id !== null && id !== void 0 ? id : experienceId,
              resourceType: resourceType
            });
            return _context.abrupt("return", result);
          case 16:
            _context.prev = 16;
            _context.t0 = _context["catch"](4);
            measure.mark(markName, 'errored');
            (0, _analytics.failUfoExperience)(ACTION_EXPERIENCE_NAME, experienceId);
            reason = typeof _context.t0 === 'string' ? _context.t0 : _context.t0 === null || _context.t0 === void 0 ? void 0 : _context.t0.message;
            fireEvent('operational.smartLinkAction.unresolved', {
              actionType: actionType !== null && actionType !== void 0 ? actionType : null,
              definitionId: definitionId,
              display: display !== null && display !== void 0 ? display : null,
              duration: (_measure$getMeasure$d2 = (_measure$getMeasure2 = measure.getMeasure(markName, 'errored')) === null || _measure$getMeasure2 === void 0 ? void 0 : _measure$getMeasure2.duration) !== null && _measure$getMeasure$d2 !== void 0 ? _measure$getMeasure$d2 : null,
              id: id !== null && id !== void 0 ? id : experienceId,
              reason: reason,
              resourceType: resourceType
            });
          case 22:
          case "end":
            return _context.stop();
        }
      }, _callee, null, [[4, 16]]);
    }));
    return function (_x) {
      return _ref3.apply(this, arguments);
    };
  }(), [fireEvent]);
};
var _default = exports.default = useInvokeClientAction;