import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useLayoutEffect, useRef, useState } from 'react';
import { ResourcedMention } from '@atlaskit/mention/element';
import ResourcedMentionWithProfilecard from './mention-with-profilecard';
var GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
export var MentionWithProviders = /*#__PURE__*/React.memo(function (_ref) {
  var accessLevel = _ref.accessLevel,
    eventHandlers = _ref.eventHandlers,
    id = _ref.id,
    mentionProvider = _ref.mentionProvider,
    profilecardProviderResolver = _ref.profilecardProvider,
    text = _ref.text,
    localId = _ref.localId;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    profilecardProvider = _useState2[0],
    setProfilecardProvider = _useState2[1];
  var mountedRef = useRef(true);
  useLayoutEffect(function () {
    mountedRef.current = true;
    return function () {
      mountedRef.current = false;
    };
  }, []);
  useLayoutEffect(function () {
    // We are not using async/await here to avoid having an intermediate Promise
    // introduced by the transpiler.
    // This will allow consumer to use a SynchronousPromise.resolve and avoid useless
    // rerendering
    profilecardProviderResolver === null || profilecardProviderResolver === void 0 || profilecardProviderResolver.then(function (result) {
      if (mountedRef.current) {
        setProfilecardProvider(result);
      }
    }).catch(function () {
      if (mountedRef.current) {
        setProfilecardProvider(null);
      }
    });
  }, [profilecardProviderResolver]);
  var MentionComponent = profilecardProvider && profilecardProviderResolver && GENERIC_USER_IDS.indexOf(id) === -1 ? ResourcedMentionWithProfilecard : ResourcedMention;
  var ssrPlaceholderId = "mention-".concat(id);
  return /*#__PURE__*/React.createElement(MentionComponent, {
    id: id,
    text: text,
    accessLevel: accessLevel,
    localId: localId,
    mentionProvider: mentionProvider
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    ,
    profilecardProvider: profilecardProvider,
    onClick: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onClick,
    onMouseEnter: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseEnter,
    onMouseLeave: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseLeave,
    ssrPlaceholderId: ssrPlaceholderId
  });
});