import React__default, { useContext, useState, useEffect } from 'react';
import { GroupChannelHandler } from '@sendbird/chat/groupChannel';
import { a as LocalizationContext } from '../../LocalizationContext-b2390bf8.js';
import { u as uuidv4 } from '../../uuid-a79b5ba8.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-4372fcec.js';
import { u as useChannelContext } from '../../ChannelProvider-498954bf.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import '../../stringSet-eefbc798.js';
import '../../index-67135027.js';
import 'prop-types';
import '../../UserProfileContext-d5787c95.js';
import '../../const-97fac210.js';
import '../../index-2dc53ac2.js';
import '../../topics-f3629ea0.js';
import '../../index-eefb44ba.js';
import '../../_rollupPluginBabelHelpers-4f8ebde4.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../compareIds-7800035f.js';
import '@sendbird/chat/message';
import '../../consts-33537265.js';
import '../../resolvedReplyType-e00d5fd9.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
