import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useState } from 'react';

// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
import uuid from 'uuid';
import { fg } from '@atlaskit/platform-feature-flags';
import { useAnalyticsEvents } from '../../../common/analytics/generated/use-analytics-events';
import * as measure from '../../../utils/performance';
import { useSmartCardActions } from '../../actions';
import { getDefinitionId, getExtensionKey, getResourceType } from '../../helpers';
import { useSmartCardState } from '../../store';
import { useScheduledRegister } from './useScheduledRegister';
var useResolveHyperlink = function useResolveHyperlink(_ref) {
  var href = _ref.href;
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  var _useState = useState(function () {
      return uuid();
    }),
    _useState2 = _slicedToArray(_useState, 1),
    id = _useState2[0];
  var state = useSmartCardState(href);
  var definitionId = getDefinitionId(state.details);
  var extensionKey = getExtensionKey(state.details);
  var resourceType = getResourceType(state.details);
  var actions = useSmartCardActions(id, href);
  var scheduledRegister = useScheduledRegister(href, actions.register);
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  useEffect(function () {
    scheduledRegister().catch(function (error) {
      throw error;
    });
  }, [scheduledRegister]);
  useEffect(function () {
    measure.mark(id, state.status);
    if (state.status !== 'pending' && state.status !== 'resolving') {
      var _state$error, _state$error2, _state$error3, _state$error4, _state$error5;
      measure.create(id, state.status);
      if (state.status === 'resolved') {
        var _measure$getMeasure$d, _measure$getMeasure;
        fireEvent('operational.hyperlink.resolved', {
          definitionId: definitionId !== null && definitionId !== void 0 ? definitionId : null,
          extensionKey: extensionKey !== null && extensionKey !== void 0 ? extensionKey : null,
          resourceType: resourceType !== null && resourceType !== void 0 ? resourceType : null,
          duration: (_measure$getMeasure$d = (_measure$getMeasure = measure.getMeasure(id, state.status)) === null || _measure$getMeasure === void 0 ? void 0 : _measure$getMeasure.duration) !== null && _measure$getMeasure$d !== void 0 ? _measure$getMeasure$d : null
        });
      } else if (fg('platform_navx_lp_invalid_url_error') ? ((_state$error = state.error) === null || _state$error === void 0 ? void 0 : _state$error.type) !== 'ResolveUnsupportedError' && ((_state$error2 = state.error) === null || _state$error2 === void 0 ? void 0 : _state$error2.type) !== 'UnsupportedError' && ((_state$error3 = state.error) === null || _state$error3 === void 0 ? void 0 : _state$error3.name) !== 'InvalidUrlError' : ((_state$error4 = state.error) === null || _state$error4 === void 0 ? void 0 : _state$error4.type) !== 'ResolveUnsupportedError' && ((_state$error5 = state.error) === null || _state$error5 === void 0 ? void 0 : _state$error5.type) !== 'UnsupportedError') {
        fireEvent('operational.hyperlink.unresolved', {
          definitionId: definitionId !== null && definitionId !== void 0 ? definitionId : null,
          extensionKey: extensionKey !== null && extensionKey !== void 0 ? extensionKey : null,
          resourceType: resourceType !== null && resourceType !== void 0 ? resourceType : null,
          reason: state.status,
          error: state.error === undefined ? null : {
            name: state.error.name,
            kind: state.error.kind,
            type: state.error.type
          }
        });
      }
    }
  }, [id, state.status, state.error, definitionId, extensionKey, resourceType, fireEvent]);
  return {
    state: state,
    actions: actions
  };
};
export default useResolveHyperlink;