"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _image = _interopRequireDefault(require("./image"));
var _link = _interopRequireDefault(require("./link"));
var _video = _interopRequireDefault(require("./video"));
var _util = require("./util");
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var ExternalNode = function (props) {
  var item = props.item,
    className = props.className,
    onImageClick = props.onImageClick,
    onImgError = props.onImgError,
    onVideoLoad = props.onVideoLoad,
    onVideoPlay = props.onVideoPlay,
    errorImage = props.errorImage,
    style = props.style,
    lazyLoad = props.lazyLoad;
  var name = item.name,
    attrs = item.attrs,
    src = item.src,
    text = item.text,
    i = item.i;
  var _a = attrs || {},
    _a$id = _a.id,
    id = _a$id === void 0 ? '' : _a$id,
    href = _a.href,
    itemClass = _a.class,
    restAttrs = (0, _tslib.__rest)(_a, ["id", "href", "class"]);
  if (text) {
    return /*#__PURE__*/(0, _react.createElement)("text", {
      className: "_text"
    }, text);
  }
  switch (name) {
    case 'a':
      return /*#__PURE__*/(0, _react.createElement)(_link.default, {
        className: (0, _classnames.default)(itemClass, className),
        hoverClass: "_hover",
        href: href,
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style)
      }, /*#__PURE__*/(0, _react.createElement)(ExternalNode, {
        item: Object.assign({
          name: 'div'
        }, item)
      }));
    case 'br':
      return /*#__PURE__*/(0, _react.createElement)("text", null, "\n      ");
    case 'img':
    case 'image':
      return /*#__PURE__*/(0, _react.createElement)(_image.default, _extends({}, restAttrs, {
        className: (0, _classnames.default)(className, itemClass),
        errorImage: errorImage,
        imgIndex: i,
        onClick: function onClick() {
          if (!item.a) {
            onImageClick(i);
          }
        },
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style),
        onError: onImgError,
        lazyLoad: lazyLoad
      }));
    case 'video':
      return /*#__PURE__*/(0, _react.createElement)(_video.default, _extends({}, restAttrs, {
        id: id,
        className: (0, _classnames.default)('_video', itemClass, className),
        src: Array.isArray(src) ? src.join('') : '',
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(Object.assign(Object.assign({}, style), _utils.isStrictMiniApp ? {
          display: 'grid'
        } : null)),
        onLoad: onVideoLoad,
        onPlay: onVideoPlay
      }));
    default:
      return /*#__PURE__*/(0, _react.createElement)("rich-text", {
        id: id,
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(Object.assign({
          display: 'inline'
        }, style)),
        nodes: [item]
      });
  }
};
var RichTextNode = /*#__PURE__*/function (_Component) {
  _inheritsLoose(RichTextNode, _Component);
  function RichTextNode() {
    return _Component.apply(this, arguments) || this;
  }
  var _proto = RichTextNode.prototype;
  _proto.render = function render() {
    var _a = this.props,
      nodes = _a.nodes,
      otherProps = (0, _tslib.__rest)(_a, ["nodes"]);
    return nodes.map(function (item, index) {
      var _item$attrs = item.attrs,
        attrs = _item$attrs === void 0 ? {} : _item$attrs;
      var _attrs$style = attrs.style,
        style = _attrs$style === void 0 ? '' : _attrs$style,
        _attrs$href = attrs.href,
        href = _attrs$href === void 0 ? '' : _attrs$href,
        nodeClassName = attrs.class,
        otherAttrs = (0, _tslib.__rest)(attrs, ["style", "href", "class"]);
      if (item.name === 'a') {
        return /*#__PURE__*/(0, _react.createElement)(_link.default, _extends({
          key: index
        }, otherAttrs, otherProps, {
          className: (0, _classnames.default)("_" + item.name, nodeClassName),
          style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)((0, _util.convertStyle)(style)),
          href: href
        }), /*#__PURE__*/(0, _react.createElement)(RichTextNode, _extends({
          nodes: item.children
        }, otherProps)));
      }
      if (item.children) {
        return /*#__PURE__*/(0, _react.createElement)("view", _extends({
          key: index
        }, otherAttrs, {
          className: (0, _classnames.default)("_" + item.name, nodeClassName),
          style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)((0, _util.convertStyle)(style))
        }), /*#__PURE__*/(0, _react.createElement)(RichTextNode, _extends({
          nodes: item.children
        }, otherProps)));
      }
      return /*#__PURE__*/(0, _react.createElement)(ExternalNode, _extends({
        key: index
      }, otherProps, {
        className: (0, _classnames.default)("_" + item.name, nodeClassName),
        item: item,
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)((0, _util.convertStyle)(style))
      }));
    });
  };
  return RichTextNode;
}(_react.Component);
var _default = RichTextNode;
exports.default = _default;