'use strict';

var React = require('react');
var index_module = require('./index.module-e3392caa.js');
var tokenize = require('./tokenize-88b021e0.js');
var Message_context = require('./Message/context.js');
var ui_MentionLabel = require('./ui/MentionLabel.js');
var ui_LinkLabel = require('./ui/LinkLabel.js');
var ui_Label = require('./index-35633fb6.js');

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

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

// this function is used to generate a unique key for token in message
// it changes with updated time and index
// messageUpdatedAt is the key part of this key generator
function keyGenerator(createdAt, messageUpdatedAt, index) {
  return `sb-msg_${createdAt}_${messageUpdatedAt}_${index}`;
}

function TextFragment(_ref) {
  let {
    tokens
  } = _ref;
  const messageStore = Message_context.useMessageContext();
  const message = messageStore === null || messageStore === void 0 ? void 0 : messageStore.message;
  const isByMe = messageStore === null || messageStore === void 0 ? void 0 : messageStore.isByMe;
  const {
    updatedAt,
    createdAt
  } = message;
  return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, tokens === null || tokens === void 0 ? void 0 : tokens.map((token, idx) => {
    const key = keyGenerator(createdAt, updatedAt, idx);
    return index_module.K(token.type).with(tokenize.TOKEN_TYPES.mention, () => /*#__PURE__*/React__default["default"].createElement("span", {
      className: "sendbird-word",
      key: key
    }, /*#__PURE__*/React__default["default"].createElement(ui_MentionLabel, {
      mentionTemplate: tokenize.USER_MENTION_PREFIX,
      mentionedUserId: token.userId,
      mentionedUserNickname: token.value,
      isByMe: isByMe
    }))).with(tokenize.TOKEN_TYPES.url, () => /*#__PURE__*/React__default["default"].createElement("span", {
      className: "sendbird-word",
      key: key
    }, /*#__PURE__*/React__default["default"].createElement(ui_LinkLabel["default"], {
      className: "sendbird-word__url",
      src: token.value,
      type: ui_Label.LabelTypography.BODY_1
    }, token.value))).otherwise(() => /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, {
      key: key
    }, tokenize.getWhiteSpacePreservedText(token.value)));
  }));
}

exports.TextFragment = TextFragment;
//# sourceMappingURL=index-db8116c0.js.map
