function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { rpx2vw4style as __rpx2vw__ } from "babel-runtime-jsx-style-transform";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _extends() { _extends = Object.assign || 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); }

import { __rest } from "tslib";
import React, { createElement, Component } from "react";
import classNames from 'classnames';
import Image from './image';
import Link from './link';
import Video from './video';
import { convertStyle } from './util';
import { isStrictMiniApp } from '../utils';

var ExternalNode = function ExternalNode(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 = __rest(_a, ["id", "href", "class"]);

  if (text) {
    return /*#__PURE__*/React.createElement("text", {
      className: "_text"
    }, text);
  }

  switch (name) {
    case 'a':
      return /*#__PURE__*/React.createElement(Link, {
        className: classNames(itemClass, className),
        hoverClass: "_hover",
        href: href,
        style: __rpx2vw__(style)
      }, /*#__PURE__*/React.createElement(ExternalNode, {
        item: Object.assign({
          name: 'div'
        }, item)
      }));

    case 'br':
      return /*#__PURE__*/React.createElement("text", null, "\n      ");

    case 'img':
    case 'image':
      return /*#__PURE__*/React.createElement(Image, _extends({}, restAttrs, {
        className: classNames(className),
        errorImage: errorImage,
        imgIndex: i,
        onClick: function onClick() {
          if (!item.a) {
            onImageClick(i);
          }
        },
        style: __rpx2vw__(style),
        onError: onImgError,
        lazyLoad: lazyLoad
      }));

    case 'video':
      return /*#__PURE__*/React.createElement(Video, _extends({}, restAttrs, {
        id: id,
        className: classNames('_video', itemClass, className),
        src: Array.isArray(src) ? src.join('') : '',
        style: __rpx2vw__(Object.assign(Object.assign({}, style), isStrictMiniApp ? {
          display: 'grid'
        } : null)),
        onLoad: onVideoLoad,
        onPlay: onVideoPlay
      }));

    default:
      return /*#__PURE__*/React.createElement("rich-text", {
        id: id,
        style: __rpx2vw__(Object.assign({
          display: 'inline'
        }, style)),
        nodes: [item]
      });
  }
};

var RichTextNode = /*#__PURE__*/function (_Component) {
  _inherits(RichTextNode, _Component);

  var _super = _createSuper(RichTextNode);

  function RichTextNode() {
    _classCallCheck(this, RichTextNode);

    return _super.apply(this, arguments);
  }

  _createClass(RichTextNode, [{
    key: "render",
    value: function render() {
      var _a = this.props,
          nodes = _a.nodes,
          otherProps = __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,
            otherAttrs = __rest(attrs, ["style", "href"]);

        if (item.name === 'a') {
          return /*#__PURE__*/React.createElement(Link, _extends({
            key: index
          }, otherAttrs, otherProps, {
            className: "_".concat(item.name),
            style: __rpx2vw__(convertStyle(style)),
            href: href
          }), /*#__PURE__*/React.createElement(RichTextNode, _extends({
            nodes: item.children
          }, otherProps)));
        }

        if (item.children) {
          return /*#__PURE__*/React.createElement("view", _extends({
            key: index
          }, otherAttrs, {
            className: "_".concat(item.name),
            style: __rpx2vw__(convertStyle(style))
          }), /*#__PURE__*/React.createElement(RichTextNode, _extends({
            nodes: item.children
          }, otherProps)));
        }

        return /*#__PURE__*/React.createElement(ExternalNode, _extends({
          key: index
        }, otherProps, {
          className: "_".concat(item.name),
          item: item,
          style: __rpx2vw__(convertStyle(style))
        }));
      });
    }
  }]);

  return RichTextNode;
}(Component);

export default RichTextNode;