"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
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 _actions = require("../../actions");
var _helpers = require("../../helpers");
var _store = require("../../store");
var _useScheduledRegister = require("./useScheduledRegister");
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 useResolveHyperlink = function useResolveHyperlink(_ref) {
  var href = _ref.href;
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  var _useState = (0, _react.useState)(function () {
      return (0, _uuid.default)();
    }),
    _useState2 = (0, _slicedToArray2.default)(_useState, 1),
    id = _useState2[0];
  var state = (0, _store.useSmartCardState)(href);
  var definitionId = (0, _helpers.getDefinitionId)(state.details);
  var extensionKey = (0, _helpers.getExtensionKey)(state.details);
  var resourceType = (0, _helpers.getResourceType)(state.details);
  var actions = (0, _actions.useSmartCardActions)(id, href);
  var scheduledRegister = (0, _useScheduledRegister.useScheduledRegister)(href, actions.register);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  (0, _react.useEffect)(function () {
    scheduledRegister().catch(function (error) {
      throw error;
    });
  }, [scheduledRegister]);
  (0, _react.useEffect)(function () {
    measure.mark(id, state.status);
    if (state.status !== 'pending' && state.status !== 'resolving') {
      var _state$error, _state$error2, _state$error3;
      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 (((_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') {
        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
  };
};
var _default = exports.default = useResolveHyperlink;