'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var groupChannel = require('@sendbird/chat/groupChannel');
var LocalizationContext = require('../../LocalizationContext-6adf6aa8.js');
var uuid = require('../../uuid-f1d9be11.js');
var ui_Label = require('../../index-bff918b2.js');
var Channel_context = require('../../ChannelProvider-5e48d37b.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
require('../../stringSet-5157206d.js');
require('../../index-6a7f94de.js');
require('prop-types');
require('../../UserProfileContext-2ff8462c.js');
require('../../const-f6c8086e.js');
require('../../index-358554d2.js');
require('../../topics-2fe6fef6.js');
require('../../index-86e680cb.js');
require('../../_rollupPluginBabelHelpers-ea66c137.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../compareIds-7b0e4960.js');
require('@sendbird/chat/message');
require('../../consts-a6f34df8.js');
require('../../resolvedReplyType-fb851d3a.js');
require('../../withSendbird.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

const TypingIndicatorText = _ref => {
  let {
    members
  } = _ref;
  const {
    stringSet
  } = React.useContext(LocalizationContext.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
  } = Channel_context.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] = React.useState(uuid.uuidv4());
  const [typingMembers, setTypingMembers] = React.useState([]);
  React.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 = uuid.uuidv4();
      const handler = new groupChannel.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["default"].createElement(ui_Label.Label, {
    className: "sendbird-conversation__footer__typing-indicator__text",
    type: ui_Label.LabelTypography.CAPTION_2,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, /*#__PURE__*/React__default["default"].createElement(TypingIndicatorText, {
    members: typingMembers
  }));
};

exports.TypingIndicatorText = TypingIndicatorText;
exports["default"] = TypingIndicator;
//# sourceMappingURL=TypingIndicator.js.map
