import React, { useMemo } from 'react';
import { ResourcedMention } from '@atlaskit/mention';
import { fg } from '@atlaskit/platform-feature-flags';
import Anchor from '@atlaskit/primitives/anchor';
import ProfileCardTrigger from '@atlaskit/profilecard/user';
import { navigateToTeamsApp } from '@atlaskit/teams-app-config/navigation';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const AT_PREFIX_REGEX = /^@/;
export default function MentionWithProfileCard({
  autoFocus,
  id,
  text,
  accessLevel,
  mentionProvider,
  profilecardProvider,
  onClick,
  onMouseEnter,
  onMouseLeave,
  localId,
  ssrPlaceholderId
}) {
  const {
    cloudId,
    renderUserMentionCard,
    resourceClient
  } = profilecardProvider;
  const actions = useMemo(() => profilecardProvider.getActions(id, text, accessLevel), [accessLevel, id, profilecardProvider, text]);
  if (fg('people-teams_migrate-user-profile-card')) {
    const mention = /*#__PURE__*/React.createElement(ResourcedMention, {
      id: id,
      text: text,
      accessLevel: accessLevel,
      localId: localId,
      mentionProvider: mentionProvider,
      onClick: onClick,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      ssrPlaceholderId: ssrPlaceholderId
    });
    if (renderUserMentionCard) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, renderUserMentionCard({
        userId: id,
        cloudId,
        children: mention
      }));
    }
    const {
      href,
      target
    } = navigateToTeamsApp({
      type: 'USER',
      payload: {
        userId: id
      },
      cloudId
    });
    return /*#__PURE__*/React.createElement(Anchor, {
      href: href,
      target: target,
      rel: "noopener noreferrer",
      testId: "mention-with-profilecard-link-fallback"
    }, mention);
  }
  return /*#__PURE__*/React.createElement(ProfileCardTrigger, {
    autoFocus: autoFocus,
    cloudId: cloudId,
    userId: id,
    resourceClient: resourceClient,
    actions: actions,
    trigger: "click",
    position: "bottom-end",
    testId: "mention-with-profilecard-trigger",
    ariaLabel: text.replace(AT_PREFIX_REGEX, ''),
    ssrPlaceholderId: ssrPlaceholderId,
    isRenderedInPortal: expValEquals('editor_a11y_7152_profile_card_tab_order', 'isEnabled', true)
  }, /*#__PURE__*/React.createElement(ResourcedMention, {
    id: id,
    text: text,
    accessLevel: accessLevel,
    localId: localId,
    mentionProvider: mentionProvider,
    onClick: onClick,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    ssrPlaceholderId: ssrPlaceholderId
  }));
}