import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { useCallback } from 'react';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { ACTION_UPDATE_METADATA_STATUS, cardAction } from '@atlaskit/linking-common';
import { fg } from '@atlaskit/platform-feature-flags';
export function usePrefetch(url, appearance) {
  var _useSmartLinkContext = useSmartLinkContext(),
    store = _useSmartLinkContext.store,
    prefetchStore = _useSmartLinkContext.prefetchStore,
    connections = _useSmartLinkContext.connections;
  var _ref = store || {},
    dispatch = _ref.dispatch,
    getState = _ref.getState;
  var _ref2 = connections || {},
    client = _ref2.client;
  return useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var isPrefetching, isFetching, needsPrefetch, response, _t;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!(!store || !prefetchStore || !connections)) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return");
        case 1:
          // If the link is already being prefetched, the prefetch store
          // should have a flag set against the URL. The prefetch store is purposefully
          // separate from the UI in order to ensure no rendering takes place;
          // all mutations which occur are purely store-based mutations.
          isPrefetching = prefetchStore[url]; // If the link has already been registered in the store, then it no
          // longer needs to be prefetched, as the normal flow of a link being in
          // the viewport and being fetched is now in motion.
          isFetching = getState()[url]; // We compute if a link needs to be prefetched thus, referencing the above.
          needsPrefetch = !isPrefetching && !isFetching;
          if (!needsPrefetch) {
            _context.next = 5;
            break;
          }
          // We specify in the prefetch store that this link no
          // longer needs to be prefetched. Data which comes back
          // from a successful prefetch flow here will be used to
          // render all URLs which are the same.
          prefetchStore[url] = true;
          // Try fetch the data for this URL from Object Resolver Service (ORS), executing batch
          // requests by domain (as usual) to ensure we minimize the amount of connections
          // we create between browser -> ORS when making network requests.
          _context.prev = 2;
          _context.next = 3;
          return client.prefetchData(url, appearance);
        case 3:
          response = _context.sent;
          // Once the data comes back, we put the link in the `resolved` status. This ensures
          // that when the link enters the viewport and is rendered, we immediately show it as
          // a Smart Link, rather than rendering a loading spinner -> immediate Smart Link.
          if (response) {
            dispatch({
              type: 'resolved',
              url: url,
              payload: response
            });
            // Put the metadata status: 'pending' for inline appearance when FG is on, so HoverCard
            // can upgrade to full block data. Otherwise 'resolved' preserves existing behaviour.
            dispatch(cardAction(ACTION_UPDATE_METADATA_STATUS, {
              url: url
            }, undefined, undefined, appearance === 'inline' && fg('platform_smartlink_inline_resolve_optimization') ? 'pending' : 'resolved'));
          }
          _context.next = 5;
          break;
        case 4:
          _context.prev = 4;
          _t = _context["catch"](2);
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[2, 4]]);
  })), [store, prefetchStore, connections, url, getState, client, dispatch, appearance]);
}