import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { CUSTOM_BASIC_EMOJI_URL, CUSTOM_BASIC_EMOJI_URL_MAPPING, CUSTOM_BIG_EMOJI_GROUP_LIST } from "./custom-emoji.mjs";
import { CUSTOM_BIG_EMOJI_URL } from "./custom-emoji.mjs";
import { DEFAULT_BASIC_EMOJI_URL, DEFAULT_BASIC_EMOJI_URL_MAPPING, BIG_EMOJI_GROUP_LIST, BASIC_EMOJI_NAME_TO_KEY_MAPPING } from "./default-emoji.mjs";
import { DEFAULT_BIG_EMOJI_URL } from "./default-emoji.mjs";
import Emoji from "./locales/zh_cn.mjs";
import { EMOJI_TYPE } from "../../../constant.mjs";
import { isWeChat } from "../../../utils/env.mjs";
const hasCustomBasicEmoji = CUSTOM_BASIC_EMOJI_URL;
const BASIC_EMOJI_URL = hasCustomBasicEmoji ? CUSTOM_BASIC_EMOJI_URL : DEFAULT_BASIC_EMOJI_URL;
const BASIC_EMOJI_URL_MAPPING = hasCustomBasicEmoji ? CUSTOM_BASIC_EMOJI_URL_MAPPING : DEFAULT_BASIC_EMOJI_URL_MAPPING;
const EMOJI_GROUP_LIST = [
  {
    emojiGroupID: 0,
    type: EMOJI_TYPE.BASIC,
    url: BASIC_EMOJI_URL,
    list: Object.keys(BASIC_EMOJI_URL_MAPPING)
  },
  ...BIG_EMOJI_GROUP_LIST,
  ...CUSTOM_BIG_EMOJI_GROUP_LIST
];
const convertKeyToEmojiName = (key) => {
  return isWeChat ? Emoji[key] : 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) => BASIC_EMOJI_NAME_TO_KEY_MAPPING[match] || match);
  }
  return txt;
};
const emojiConfig = {
  emojiBaseUrl: BASIC_EMOJI_URL,
  emojiUrlMapping: BASIC_EMOJI_URL_MAPPING,
  emojiNameMapping: {
    ...Emoji
  }
};
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;
};
export {
  BASIC_EMOJI_URL_MAPPING,
  CUSTOM_BASIC_EMOJI_URL,
  CUSTOM_BASIC_EMOJI_URL_MAPPING,
  CUSTOM_BIG_EMOJI_URL,
  DEFAULT_BIG_EMOJI_URL,
  EMOJI_GROUP_LIST,
  convertKeyToEmojiName,
  emojiConfig,
  parseTextToRenderArray,
  transformTextWithEmojiNamesToKeys,
  transformTextWithKeysToEmojiNames
};
