import React, { useMemo } from 'react';
import Loadable from 'react-loadable';
import { ResourcedMention } from '@atlaskit/mention';
import { fg } from '@atlaskit/platform-feature-flags';
// eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- TODO: migrate to @atlaskit/primitives/compiled
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';
// Lazy-loaded so the agent profile card chunk (and its `@atlaskit/rovo-agent-components`
// dependency) stays off the critical path — it is only fetched when an agent mention
// (`userType === 'APP'`) actually renders, never for person mentions.
// Uses react-loadable to match editor-common's code-splitting factory (see src/icons/index.ts).
const AgentProfileCardTrigger = Loadable({
  loader: () => import( /* webpackChunkName: "@atlaskit-internal_profilecard/agent-profile-card-trigger" */
  '@atlaskit/profilecard/agent-profile-card-trigger').then(mod => mod.AgentProfileCardTrigger),
  loading: () => null
});

// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const AT_PREFIX_REGEX = /^@/;
/**
 * Renders a mention chip wrapped in the appropriate profile card trigger.
 * 1. Agent mentions (`userType === 'APP'`, behind `rovo_chat_agent_selection`) open the Rovo agent profile card on click.
 * 2. Otherwise renders the person profile card (either via the provider's `renderUserMentionCard`/link fallback or the default user `ProfileCardTrigger`).
 */
export default function MentionWithProfileCard({
  autoFocus,
  id,
  text,
  accessLevel,
  mentionProvider,
  profilecardProvider,
  onClick,
  onMouseEnter,
  onMouseLeave,
  localId,
  ssrPlaceholderId,
  userType
}) {
  const {
    cloudId,
    renderUserMentionCard,
    resourceClient
  } = profilecardProvider;
  const actions = useMemo(() => profilecardProvider.getActions(id, text, accessLevel), [accessLevel, id, profilecardProvider, text]);
  const mention = /*#__PURE__*/React.createElement(ResourcedMention, {
    id: id,
    text: text,
    accessLevel: accessLevel,
    localId: localId,
    mentionProvider: mentionProvider,
    onClick: onClick,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    ssrPlaceholderId: ssrPlaceholderId
  });

  // Agent mentions (userType 'APP') open the Rovo agent profile card on click.
  if (userType === 'APP' && cloudId && fg('rovo_chat_agent_selection')) {
    return /*#__PURE__*/React.createElement(AgentProfileCardTrigger, {
      agentId: id,
      cloudId: cloudId,
      resourceClient: resourceClient,
      trigger: "click"
    }, mention);
  }
  if (fg('people-teams_migrate-user-profile-card')) {
    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)
  }, mention);
}