'use strict';

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

var React = require('react');
var groupChannel = require('@sendbird/chat/groupChannel');
var LocalizationContext = require('../../LocalizationContext-b42fa07d.js');
var uuid = require('../../uuid-5ab4f88b.js');
var ui_Label = require('../../index-72e412f7.js');
var Channel_context = require('../../ChannelProvider-2bfafa25.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
require('../../stringSet-4f30313f.js');
require('../../index-97005534.js');
require('prop-types');
require('../../UserProfileContext-c87514d3.js');
require('../../index-849f44b0.js');
require('../../topics-3226c6ee.js');
require('../../index-0b8a1074.js');
require('../../_rollupPluginBabelHelpers-3a9d1641.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../compareIds-a7029d6a.js');
require('../../const-68cf2709.js');
require('@sendbird/chat/message');
require('../../consts-f71467c8.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
