"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 _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
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(params) {
      var url, _params$isReloading, isReloading, _params$isMetadataReq, isMetadataRequest, _params$id, id, appearance, _ref2, details, hasData, metadataStatus;
      return _regenerator.default.wrap(function (_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            url = params.url, _params$isReloading = params.isReloading, isReloading = _params$isReloading === void 0 ? false : _params$isReloading, _params$isMetadataReq = params.isMetadataRequest, isMetadataRequest = _params$isMetadataReq === void 0 ? false : _params$isMetadataReq, _params$id = params.id, id = _params$id === void 0 ? '' : _params$id, appearance = params.appearance;
            _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;
            }
            metadataStatus = appearance === 'inline' && !isMetadataRequest && (0, _platformFeatureFlags.fg)('platform_smartlink_inline_resolve_optimization') ? 'pending' : undefined;
            return _context.abrupt("return", connections.client.fetchData(url, isReloading, appearance).then(function (response) {
              return handleResolvedLinkResponse(url, response, isReloading, isMetadataRequest, metadataStatus);
            }).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;