"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.processEmojiTextMsg = processEmojiTextMsg;
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.array.index-of.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.includes.js");
var _react = _interopRequireDefault(require("react"));
var svgExports = _interopRequireWildcard(require("../icon/svg-exports"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var emojiMap = Object.keys(svgExports);

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

  // 表情图片
  var imgTemplate = function imgTemplate(content) {
    return (
      /*#__PURE__*/
      // TODO: 需要替换为真实的表情图片
      (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: /*#__PURE__*/(0, _jsxRuntime.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__*/(0, _jsxRuntime.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__*/(0, _jsxRuntime.jsx)("div", {
    className: className,
    children: msgElements.map(function (el, i) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react["default"].Fragment, {
        children: el
      }, i);
    })
  });
}