"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = MentionWithProfileCard;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _react = _interopRequireWildcard(require("react"));
var _reactLoadable = _interopRequireDefault(require("react-loadable"));
var _mention = require("@atlaskit/mention");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _anchor = _interopRequireDefault(require("@atlaskit/primitives/anchor"));
var _user = _interopRequireDefault(require("@atlaskit/profilecard/user"));
var _navigation = require("@atlaskit/teams-app-config/navigation");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != (0, _typeof2.default)(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } // eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- TODO: migrate to @atlaskit/primitives/compiled
// 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).
var AgentProfileCardTrigger = (0, _reactLoadable.default)({
  loader: function loader() {
    return Promise.resolve().then(function () {
      return _interopRequireWildcard(require( /* webpackChunkName: "@atlaskit-internal_profilecard/agent-profile-card-trigger" */
      '@atlaskit/profilecard/agent-profile-card-trigger'));
    }).then(function (mod) {
      return mod.AgentProfileCardTrigger;
    });
  },
  loading: function loading() {
    return null;
  }
});

// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var 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`).
 */
function MentionWithProfileCard(_ref) {
  var autoFocus = _ref.autoFocus,
    id = _ref.id,
    text = _ref.text,
    accessLevel = _ref.accessLevel,
    mentionProvider = _ref.mentionProvider,
    profilecardProvider = _ref.profilecardProvider,
    onClick = _ref.onClick,
    onMouseEnter = _ref.onMouseEnter,
    onMouseLeave = _ref.onMouseLeave,
    localId = _ref.localId,
    ssrPlaceholderId = _ref.ssrPlaceholderId,
    userType = _ref.userType;
  var cloudId = profilecardProvider.cloudId,
    renderUserMentionCard = profilecardProvider.renderUserMentionCard,
    resourceClient = profilecardProvider.resourceClient;
  var actions = (0, _react.useMemo)(function () {
    return profilecardProvider.getActions(id, text, accessLevel);
  }, [accessLevel, id, profilecardProvider, text]);
  var mention = /*#__PURE__*/_react.default.createElement(_mention.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 && (0, _platformFeatureFlags.fg)('rovo_chat_agent_selection')) {
    return /*#__PURE__*/_react.default.createElement(AgentProfileCardTrigger, {
      agentId: id,
      cloudId: cloudId,
      resourceClient: resourceClient,
      trigger: "click"
    }, mention);
  }
  if ((0, _platformFeatureFlags.fg)('people-teams_migrate-user-profile-card')) {
    if (renderUserMentionCard) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderUserMentionCard({
        userId: id,
        cloudId: cloudId,
        children: mention
      }));
    }
    var _navigateToTeamsApp = (0, _navigation.navigateToTeamsApp)({
        type: 'USER',
        payload: {
          userId: id
        },
        cloudId: cloudId
      }),
      href = _navigateToTeamsApp.href,
      target = _navigateToTeamsApp.target;
    return /*#__PURE__*/_react.default.createElement(_anchor.default, {
      href: href,
      target: target,
      rel: "noopener noreferrer",
      testId: "mention-with-profilecard-link-fallback"
    }, mention);
  }
  return /*#__PURE__*/_react.default.createElement(_user.default, {
    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: (0, _expValEquals.expValEquals)('editor_a11y_7152_profile_card_tab_order', 'isEnabled', true)
  }, mention);
}