import React__default, { useRef, useState, useCallback } from 'react';
import ContextMenu, { MenuItems } from './ContextMenu.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../index-f50d7ab0.js';
import UserProfile from './UserProfile.js';
import useSendbirdStateContext from '../useSendbirdStateContext.js';
import '../_rollupPluginBabelHelpers-cc035350.js';
import 'react-dom';
import './SortByRow.js';
import '../uuid-8b344dd2.js';
import '../index-99ab01f6.js';
import '../utils/message/getOutgoingMessageState.js';
import 'prop-types';
import '../stringSet-e9bb0333.js';
import '../LocalizationContext-1701e5c8.js';
import '../index-ff8f2f61.js';
import '../UserProfileContext-74e9a128.js';
import '../sendbirdSelectors.js';
import '../topics-0b30f52b.js';
import '../utils-05aa9060.js';
import './Avatar.js';
import './ImageRenderer.js';
import './Icon.js';
import './Button.js';
import '../withSendbird.js';

function MentionLabel(props) {
  var _sendbirdState$config, _sendbirdState$stores, _sendbirdState$stores2;
  const {
    mentionTemplate,
    mentionedUserId,
    mentionedUserNickname,
    isByMe
  } = props;
  const mentionRef = useRef();
  const sendbirdState = useSendbirdStateContext();
  const userId = sendbirdState === null || sendbirdState === void 0 ? void 0 : (_sendbirdState$config = sendbirdState.config) === null || _sendbirdState$config === void 0 ? void 0 : _sendbirdState$config.userId;
  const sdk = sendbirdState === null || sendbirdState === void 0 ? void 0 : (_sendbirdState$stores = sendbirdState.stores) === null || _sendbirdState$stores === void 0 ? void 0 : (_sendbirdState$stores2 = _sendbirdState$stores.sdkStore) === null || _sendbirdState$stores2 === void 0 ? void 0 : _sendbirdState$stores2.sdk;
  const amIBeingMentioned = userId === mentionedUserId;
  const [user, setUser] = useState();
  const fetchUser = useCallback(toggleDropdown => {
    if (user || !(sdk !== null && sdk !== void 0 && sdk.createApplicationUserListQuery)) {
      toggleDropdown();
      return;
    }
    const query = sdk === null || sdk === void 0 ? void 0 : sdk.createApplicationUserListQuery({
      userIdsFilter: [mentionedUserId]
    });
    query.next().then(members => {
      if ((members === null || members === void 0 ? void 0 : members.length) > 0) {
        setUser(members[0]);
      }
      toggleDropdown();
    });
  }, [sdk, mentionedUserId]);
  return /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement("a", {
      className: `
            sendbird-word__mention
            ${amIBeingMentioned ? 'sendbird-word__mention--me' : ''}
          `,
      onClick: () => fetchUser(toggleDropdown),
      ref: mentionRef,
      "data-userid": mentionedUserId,
      "data-nickname": mentionedUserNickname,
      "data-sb-mention": true
    }, /*#__PURE__*/React__default.createElement(Label, {
      type: LabelTypography.CAPTION_1,
      color: isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1
    }, `${mentionTemplate}${mentionedUserNickname}`)),
    menuItems: closeDropdown => /*#__PURE__*/React__default.createElement(MenuItems
    /**
    * parentRef: For catching location(x, y) of MenuItems
    * parentContainRef: For toggling more options(menus & reactions)
    */, {
      parentRef: mentionRef,
      parentContainRef: mentionRef,
      closeDropdown: closeDropdown,
      style: {
        paddingTop: '0px',
        paddingBottom: '0px'
      }
    }, /*#__PURE__*/React__default.createElement(UserProfile, {
      user: user,
      onSuccess: closeDropdown,
      currentUserId: userId
    }))
  });
}

export { MentionLabel as default };
//# sourceMappingURL=MentionLabel.js.map
