"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const TUIChatEngine = require("@tencentcloud/chat-uikit-engine");
const customEmoji = require("./custom-emoji.js");
const defaultEmoji = require("./default-emoji.js");
const zh_cn = require("./locales/zh_cn.js");
const constant = require("../../../constant.js");
const env = require("../../../utils/env.js");
const hasCustomBasicEmoji = customEmoji.CUSTOM_BASIC_EMOJI_URL;
const BASIC_EMOJI_URL = hasCustomBasicEmoji ? customEmoji.CUSTOM_BASIC_EMOJI_URL : defaultEmoji.DEFAULT_BASIC_EMOJI_URL;
const BASIC_EMOJI_URL_MAPPING = hasCustomBasicEmoji ? customEmoji.CUSTOM_BASIC_EMOJI_URL_MAPPING : defaultEmoji.DEFAULT_BASIC_EMOJI_URL_MAPPING;
const EMOJI_GROUP_LIST = [
  {
    emojiGroupID: 0,
    type: constant.EMOJI_TYPE.BASIC,
    url: BASIC_EMOJI_URL,
    list: Object.keys(BASIC_EMOJI_URL_MAPPING)
  },
  ...defaultEmoji.BIG_EMOJI_GROUP_LIST,
  ...customEmoji.CUSTOM_BIG_EMOJI_GROUP_LIST
];
const convertKeyToEmojiName = (key) => {
  return env.isWeChat ? zh_cn.default[key] : TUIChatEngine.TUITranslateService.t(`Emoji.${key}`);
};
const transformTextWithKeysToEmojiNames = (text) => {
  if (!text) {
    return "";
  }
  const reg = /(\[.+?\])/g;
  let txt = text;
  if (reg.test(text)) {
    txt = text.replace(reg, (match) => BASIC_EMOJI_URL_MAPPING[match] ? convertKeyToEmojiName(match) : match);
  }
  return txt;
};
const transformTextWithEmojiNamesToKeys = (text) => {
  if (!text) {
    return "";
  }
  const reg = /(\[.+?\])/g;
  let txt = text;
  if (reg.test(text)) {
    txt = text.replace(reg, (match) => defaultEmoji.BASIC_EMOJI_NAME_TO_KEY_MAPPING[match] || match);
  }
  return txt;
};
const emojiConfig = {
  emojiBaseUrl: BASIC_EMOJI_URL,
  emojiUrlMapping: BASIC_EMOJI_URL_MAPPING,
  emojiNameMapping: {
    ...zh_cn.default
  }
};
const parseTextToRenderArray = (text) => {
  const emojiRegex = /\[([^\]]+)\]/g;
  const result = [];
  let match;
  let lastIndex = 0;
  while ((match = emojiRegex.exec(text)) !== null) {
    const startIndex = match.index;
    const endIndex = emojiRegex.lastIndex;
    const emojiKey = match[0];
    if (startIndex > lastIndex) {
      result.push({ type: "text", content: text.substring(lastIndex, startIndex) });
    }
    const emojiUrl = BASIC_EMOJI_URL + BASIC_EMOJI_URL_MAPPING[emojiKey];
    if (emojiUrl) {
      result.push({ type: "image", content: emojiUrl, emojiKey });
    } else {
      result.push({ type: "text", content: emojiKey });
    }
    lastIndex = endIndex;
    emojiRegex.lastIndex = lastIndex;
  }
  if (lastIndex < text.length) {
    result.push({ type: "text", content: text.substring(lastIndex) });
  }
  return result;
};
exports.CUSTOM_BASIC_EMOJI_URL = customEmoji.CUSTOM_BASIC_EMOJI_URL;
exports.CUSTOM_BASIC_EMOJI_URL_MAPPING = customEmoji.CUSTOM_BASIC_EMOJI_URL_MAPPING;
exports.CUSTOM_BIG_EMOJI_URL = customEmoji.CUSTOM_BIG_EMOJI_URL;
exports.DEFAULT_BIG_EMOJI_URL = defaultEmoji.DEFAULT_BIG_EMOJI_URL;
exports.BASIC_EMOJI_URL_MAPPING = BASIC_EMOJI_URL_MAPPING;
exports.EMOJI_GROUP_LIST = EMOJI_GROUP_LIST;
exports.convertKeyToEmojiName = convertKeyToEmojiName;
exports.emojiConfig = emojiConfig;
exports.parseTextToRenderArray = parseTextToRenderArray;
exports.transformTextWithEmojiNamesToKeys = transformTextWithEmojiNamesToKeys;
exports.transformTextWithKeysToEmojiNames = transformTextWithKeysToEmojiNames;
