import "core-js/modules/es.array.includes.js";
import "core-js/modules/es.array.index-of.js";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.array.slice.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.includes.js";
import React from 'react';
import * as svgExports from '../icon/svg-exports';
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
var emojiMap = Object.keys(svgExports);

/**
 *
 * @param msg
 * @description 文本消息处理，将表情文本转换为表情图片，返回 ReactNode
 * @returns
 */
export function processEmojiTextMsg(msg, className) {
  var inputString = msg;
  if (!inputString) {
    return null;
  }

  // 表情图片
  var imgTemplate = function imgTemplate(content) {
    return (
      /*#__PURE__*/
      // TODO: 需要替换为真实的表情图片
      _jsx(_Fragment, {
        children: /*#__PURE__*/_jsx("img", {
          style: {
            display: 'inline-block',
            height: 24,
            width: 24
          },
          src: "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
        })
      })
    );
  };

  // 文本
  var txtTemplate = function txtTemplate(content) {
    return /*#__PURE__*/_jsx("span", {
      children: content
    });
  };
  var pattern = /\[([a-zA-Z0-9_]+)\]/g;
  var msgElements = [];
  var lastPos = 0;
  var match;
  while ((match = pattern.exec(inputString)) !== null) {
    var start = match.index;
    var end = pattern.lastIndex;
    var emojiName = match[1];

    // TODO: 连调后删除
    // console.log({ msg, inputString, emojiName }, 'processEmojiTextMsg - while');
    // 匹配表情前的文本
    var prefixStr = inputString.slice(lastPos, start);
    prefixStr.length > 0 && msgElements.push(txtTemplate(prefixStr));

    // 存在 emoji
    if (emojiMap.includes(emojiName)) {
      msgElements.push(imgTemplate(emojiMap[emojiMap.indexOf(emojiName)]));
    } else {
      // 当作普通文本处理
      var contentStr = inputString.slice(start, end);
      contentStr.length > 0 && msgElements.push(txtTemplate(contentStr));
    }
    lastPos = end;
  }
  var suffixStr = inputString.slice(lastPos);
  // 匹配表情后剩余文本
  suffixStr.length > 0 && msgElements.push(txtTemplate(suffixStr));

  // console.log({ result: msgElements }, ' processEmojiTextMsg function');
  return /*#__PURE__*/_jsx("div", {
    className: className,
    children: msgElements.map(function (el, i) {
      return /*#__PURE__*/_jsx(React.Fragment, {
        children: el
      }, i);
    })
  });
}