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';
const ACTION_EXPERIENCE_NAME = 'smart-link-action-invocation';

/**
 * Invoke client action such as preview, download and open link
 */
const useInvokeClientAction = ({
  fireEvent: fireEventProp
}) => {
  const {
    fireEvent: defaultFireEvent
  } = useAnalyticsEvents();
  const fireEvent = fireEventProp !== null && fireEventProp !== void 0 ? fireEventProp : defaultFireEvent;
  return useCallback(async ({
    actionSubjectId,
    actionType,
    actionFn,
    definitionId = null,
    extensionKey,
    display,
    id,
    prompt,
    resourceType = null
  }) => {
    // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
    const experienceId = uuid();

    // Begin performance instrumentation.
    const markName = `${experienceId}-${actionType}`;
    measure.mark(markName, 'pending');
    try {
      var _measure$getMeasure$d, _measure$getMeasure;
      // Begin UFO experience
      startUfoExperience(ACTION_EXPERIENCE_NAME, experienceId, {
        actionType,
        display,
        extensionKey,
        invokeType: 'client'
      });

      // Begin analytics instrumentation.
      if (actionSubjectId !== undefined) {
        fireEvent(`ui.button.clicked.${actionSubjectId}`, {
          actionType: actionType !== null && actionType !== void 0 ? actionType : null,
          definitionId,
          display: display !== null && display !== void 0 ? display : null,
          id: id !== null && id !== void 0 ? id : experienceId,
          resourceType,
          prompt
        });
      }
      // Invoke action
      const result = await actionFn();
      measure.mark(markName, 'resolved');
      succeedUfoExperience(ACTION_EXPERIENCE_NAME, experienceId);
      fireEvent('operational.smartLinkAction.resolved', {
        actionType: actionType !== null && actionType !== void 0 ? actionType : null,
        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
      });
      return result;
    } catch (err) {
      var _measure$getMeasure$d2, _measure$getMeasure2;
      measure.mark(markName, 'errored');
      failUfoExperience(ACTION_EXPERIENCE_NAME, experienceId);
      const reason = typeof err === 'string' ? err : err === null || err === void 0 ? void 0 : err.message;
      fireEvent('operational.smartLinkAction.unresolved', {
        actionType: actionType !== null && actionType !== void 0 ? actionType : null,
        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,
        resourceType
      });
    }
  }, [fireEvent]);
};
export default useInvokeClientAction;