import React__default, { useContext, useState, useEffect } from 'react';
import { GroupChannelHandler } from '@sendbird/chat/groupChannel';
import { a as LocalizationContext } from '../../LocalizationContext-1701e5c8.js';
import { u as uuidv4 } from '../../uuid-8b344dd2.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-f50d7ab0.js';
import { u as useChannelContext } from '../../ChannelProvider-1dc36682.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import '../../stringSet-e9bb0333.js';
import '../../index-ff8f2f61.js';
import 'prop-types';
import '../../UserProfileContext-74e9a128.js';
import '../../const-7c266a95.js';
import '../../index-5a62f6b7.js';
import '../../topics-0b30f52b.js';
import '../../index-99ab01f6.js';
import '../../_rollupPluginBabelHelpers-cc035350.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../compareIds-367bb030.js';
import '@sendbird/chat/message';
import '../../consts-44eaef6b.js';
import '../../resolvedReplyType-6bb8f548.js';
import '../../withSendbird.js';

const TypingIndicatorText = _ref => {
  let {
    members
  } = _ref;
  const {
    stringSet
  } = useContext(LocalizationContext);
  if (!members || members.length === 0) {
    return '';
  }
  if (members && members.length === 1) {
    return `${members[0].nickname} ${stringSet.TYPING_INDICATOR__IS_TYPING}`;
  }
  if (members && members.length === 2) {
    return `${members[0].nickname} ${stringSet.TYPING_INDICATOR__AND} ${members[1].nickname} ${stringSet.TYPING_INDICATOR__ARE_TYPING}`;
  }
  return stringSet.TYPING_INDICATOR__MULTIPLE_TYPING;
};
const TypingIndicator = () => {
  var _globalStore$stores, _globalStore$stores$s, _globalStore$config;
  const {
    channelUrl
  } = useChannelContext();
  const globalStore = useSendbirdStateContext();
  const sb = globalStore === null || globalStore === void 0 ? void 0 : (_globalStore$stores = globalStore.stores) === null || _globalStore$stores === void 0 ? void 0 : (_globalStore$stores$s = _globalStore$stores.sdkStore) === null || _globalStore$stores$s === void 0 ? void 0 : _globalStore$stores$s.sdk;
  const logger = globalStore === null || globalStore === void 0 ? void 0 : (_globalStore$config = globalStore.config) === null || _globalStore$config === void 0 ? void 0 : _globalStore$config.logger;
  const [handlerId, setHandlerId] = useState(uuidv4());
  const [typingMembers, setTypingMembers] = useState([]);
  useEffect(() => {
    var _sb$groupChannel;
    if (sb !== null && sb !== void 0 && (_sb$groupChannel = sb.groupChannel) !== null && _sb$groupChannel !== void 0 && _sb$groupChannel.addGroupChannelHandler) {
      sb.groupChannel.removeGroupChannelHandler(handlerId);
      const newHandlerId = uuidv4();
      const handler = new GroupChannelHandler({
        onTypingStatusUpdated: groupChannel => {
          // there is a possible warning in here - setState called after unmount
          logger.info('Channel > Typing Indicator: onTypingStatusUpdated', groupChannel);
          if (groupChannel.url === channelUrl) {
            const members = groupChannel.getTypingUsers();
            setTypingMembers(members);
          }
        }
      });
      sb.groupChannel.addGroupChannelHandler(newHandlerId, handler);
      setHandlerId(newHandlerId);
    }
    return () => {
      var _sb$groupChannel2;
      setTypingMembers([]);
      if (sb !== null && sb !== void 0 && (_sb$groupChannel2 = sb.groupChannel) !== null && _sb$groupChannel2 !== void 0 && _sb$groupChannel2.removeGroupChannelHandler) {
        sb.groupChannel.removeGroupChannelHandler(handlerId);
      }
    };
  }, [channelUrl]);
  return /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-conversation__footer__typing-indicator__text",
    type: LabelTypography.CAPTION_2,
    color: LabelColors.ONBACKGROUND_2
  }, /*#__PURE__*/React__default.createElement(TypingIndicatorText, {
    members: typingMembers
  }));
};

export { TypingIndicatorText, TypingIndicator as default };
//# sourceMappingURL=TypingIndicator.js.map
