"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.parseHtml = void 0;
var _ui = require("@mirai/ui");
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const parseHtml = function () {
  let html = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  let {
    small
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const parser = new DOMParser();
  const body = parser.parseFromString(html, 'text/html').querySelector('body');
  const traverseNodes = function () {
    let nodes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    const components = [];
    nodes.forEach((node, index) => {
      const {
        childNodes,
        nodeType,
        tagName,
        textContent
      } = node;
      const key = "".concat(tagName).concat(index);
      if (nodeType === Node.TEXT_NODE) {
        components.push(textContent);
      } else if (nodeType === Node.ELEMENT_NODE) {
        if (tagName === 'P') {
          components.push(/*#__PURE__*/_react.default.createElement(_ui.Text, {
            key,
            small,
            wide: true
          }, traverseNodes(childNodes)));
        } else if (tagName === 'BR') {
          components.push(/*#__PURE__*/_react.default.createElement(_ui.View, {
            key,
            wide: true
          }));
        } else if (tagName === 'B') {
          components.push(/*#__PURE__*/_react.default.createElement(_ui.Text, {
            key,
            small,
            style: {
              display: 'inline',
              fontWeight: 'var(--mirai-ui-font-bold-weight)'
            }
          }, traverseNodes(childNodes)));
        } else if (tagName === 'SPAN') {
          components.push(/*#__PURE__*/_react.default.createElement(_ui.Text, {
            key,
            small,
            style: {
              display: 'inline'
            }
          }, traverseNodes(childNodes)));
        } else if (tagName === 'A') {
          const href = node.getAttribute('href');
          components.push(/*#__PURE__*/_react.default.createElement(href ? _ui.Action : _react.default.Fragment, _objectSpread(_objectSpread({}, href ? {
            href,
            small,
            style: {
              display: 'inline-flex'
            },
            target: '_blank'
          } : undefined), {}, {
            key
          }), textContent));
        }
      } else {
        traverseNodes(childNodes);
      }
    });
    return components;
  };
  return traverseNodes(body.childNodes);
};
exports.parseHtml = parseHtml;
//# sourceMappingURL=parseHtml.js.map