import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { useCallback } from 'react';
import { isEntityPresent } from '@atlaskit/link-extractors';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { fg } from '@atlaskit/platform-feature-flags';
import { SmartLinkStatus } from '../../../constants';
import { addMetadataToExperience } from '../../analytics';
import useResponse from '../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 = useSmartLinkContext(),
    store = _useSmartLinkContext.store,
    connections = _useSmartLinkContext.connections;
  var getState = store.getState;
  var _useResponse = useResponse(),
    handleResolvedLinkResponse = _useResponse.handleResolvedLinkResponse,
    handleResolvedLinkError = _useResponse.handleResolvedLinkError;
  return useCallback( /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
      var url, _params$isReloading, isReloading, _params$isMetadataReq, isMetadataRequest, _params$id, id, appearance, _ref2, details, hasData, metadataStatus;
      return _regeneratorRuntime.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: SmartLinkStatus.Pending,
              details: undefined
            }, details = _ref2.details;
            hasData = !!(details && details.data || isEntityPresent(details));
            if (!(isReloading || !hasData || isMetadataRequest)) {
              _context.next = 1;
              break;
            }
            metadataStatus = appearance === 'inline' && !isMetadataRequest && 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:
            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]);
};
export default useResolve;