"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
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 _linkExtractors = require("@atlaskit/link-extractors");
var _linkProvider = require("@atlaskit/link-provider");
var _constants = require("../../../constants");
var _analytics = require("../../analytics");
var _useResponse2 = _interopRequireDefault(require("../use-response"));
var useResolve = function useResolve() {
  // Request JSON-LD data for the card from ORS, if it has extended
  // its cache lifespan OR there is no data for it currently. Once the data
  // has come back asynchronously, call the useResponse callback to
  // dispatch the resolved action for the card.
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    store = _useSmartLinkContext.store,
    connections = _useSmartLinkContext.connections;
  var getState = store.getState;
  var _useResponse = (0, _useResponse2.default)(),
    handleResolvedLinkResponse = _useResponse.handleResolvedLinkResponse,
    handleResolvedLinkError = _useResponse.handleResolvedLinkError;
  return (0, _react.useCallback)( /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(url) {
      var isReloading,
        isMetadataRequest,
        id,
        _ref2,
        details,
        hasData,
        _args = arguments;
      return _regenerator.default.wrap(function (_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            isReloading = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
            isMetadataRequest = _args.length > 2 && _args[2] !== undefined ? _args[2] : false;
            id = _args.length > 3 && _args[3] !== undefined ? _args[3] : '';
            _ref2 = getState()[url] || {
              status: _constants.SmartLinkStatus.Pending,
              details: undefined
            }, details = _ref2.details;
            hasData = !!(details && details.data || (0, _linkExtractors.isEntityPresent)(details));
            if (!(isReloading || !hasData || isMetadataRequest)) {
              _context.next = 1;
              break;
            }
            return _context.abrupt("return", connections.client.fetchData(url, isReloading).then(function (response) {
              return handleResolvedLinkResponse(url, response, isReloading, isMetadataRequest);
            }).catch(function (error) {
              return handleResolvedLinkError(url, error, undefined, isMetadataRequest);
            }));
          case 1:
            (0, _analytics.addMetadataToExperience)('smart-link-rendered', id, {
              cached: true
            });
          case 2:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }(), [connections.client, getState, handleResolvedLinkError, handleResolvedLinkResponse]);
};
var _default = exports.default = useResolve;