import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { useCallback } from 'react';

// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
import uuid from 'uuid';
import { useAnalyticsEvents } from '../../../common/analytics/generated/use-analytics-events';
import * as measure from '../../../utils/performance';
import { failUfoExperience, startUfoExperience, succeedUfoExperience } from '../../analytics';
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 = useAnalyticsEvents(),
    defaultFireEvent = _useAnalyticsEvents.fireEvent;
  var fireEvent = fireEventProp !== null && fireEventProp !== void 0 ? fireEventProp : defaultFireEvent;
  return useCallback( /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref2) {
      var actionSubjectId, actionType, actionFn, _ref2$definitionId, definitionId, extensionKey, display, id, prompt, _ref2$resourceType, resourceType, experienceId, markName, _measure$getMeasure$d, _measure$getMeasure, result, _measure$getMeasure$d2, _measure$getMeasure2, reason, _t;
      return _regeneratorRuntime.wrap(function (_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, prompt = _ref2.prompt, _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 = uuid(); // Begin performance instrumentation.
            markName = "".concat(experienceId, "-").concat(actionType);
            measure.mark(markName, 'pending');
            _context.prev = 1;
            // Begin UFO experience
            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,
                prompt: prompt
              });
            }
            // Invoke action
            _context.next = 2;
            return actionFn();
          case 2:
            result = _context.sent;
            measure.mark(markName, 'resolved');
            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 3:
            _context.prev = 3;
            _t = _context["catch"](1);
            measure.mark(markName, 'errored');
            failUfoExperience(ACTION_EXPERIENCE_NAME, experienceId);
            reason = typeof _t === 'string' ? _t : _t === null || _t === void 0 ? void 0 : _t.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 4:
          case "end":
            return _context.stop();
        }
      }, _callee, null, [[1, 3]]);
    }));
    return function (_x) {
      return _ref3.apply(this, arguments);
    };
  }(), [fireEvent]);
};
export default useInvokeClientAction;