import React, { useLayoutEffect, useRef, useState } from 'react';
import { ResourcedMention } from '@atlaskit/mention/element';
import ResourcedMentionWithProfilecard from './mention-with-profilecard';
const GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
export const MentionWithProviders = /*#__PURE__*/React.memo(({
  accessLevel,
  disabledTooltip,
  eventHandlers,
  id,
  isDisabled,
  mentionProvider,
  profilecardProvider: profilecardProviderResolver,
  text,
  localId,
  userType
}) => {
  const [profilecardProvider, setProfilecardProvider] = useState(null);
  const mountedRef = useRef(true);
  useLayoutEffect(() => {
    mountedRef.current = true;
    return () => {
      mountedRef.current = false;
    };
  }, []);
  useLayoutEffect(() => {
    // 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 ? void 0 : profilecardProviderResolver.then(result => {
      if (mountedRef.current) {
        setProfilecardProvider(result);
      }
    }).catch(() => {
      if (mountedRef.current) {
        setProfilecardProvider(null);
      }
    });
  }, [profilecardProviderResolver]);
  const MentionComponent = profilecardProvider && profilecardProviderResolver && GENERIC_USER_IDS.indexOf(id) === -1 ? ResourcedMentionWithProfilecard : ResourcedMention;
  const ssrPlaceholderId = `mention-${id}`;

  // A disabled mention is non-interactive, so it bypasses the profile-card
  // wrapper and renders the disabled resourced mention directly.
  if (isDisabled) {
    return /*#__PURE__*/React.createElement(ResourcedMention, {
      id: id,
      text: text,
      accessLevel: accessLevel,
      localId: localId,
      mentionProvider: mentionProvider,
      isDisabled: true,
      disabledTooltip: disabledTooltip,
      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
    });
  }
  return /*#__PURE__*/React.createElement(MentionComponent, {
    id: id,
    text: text,
    accessLevel: accessLevel,
    localId: localId,
    userType: userType,
    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
  });
});