"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = getReactNodeText;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = require("react");

var _reactDom = require("react-dom");

var _isString = _interopRequireDefault(require("lodash/isString"));

var _isNumber = _interopRequireDefault(require("lodash/isNumber"));

var _toString = _interopRequireDefault(require("lodash/toString"));

function getReactNodeText(_x) {
  return _getReactNodeText.apply(this, arguments);
}

function _getReactNodeText() {
  _getReactNodeText = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(node) {
    var textDiv, textContent;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!node) {
              _context.next = 13;
              break;
            }

            if (!((0, _isString["default"])(node) || (0, _isNumber["default"])(node))) {
              _context.next = 3;
              break;
            }

            return _context.abrupt("return", String(node));

          case 3:
            if (!(typeof window !== 'undefined' && /*#__PURE__*/(0, _react.isValidElement)(node))) {
              _context.next = 12;
              break;
            }

            textDiv = document.createElement('div');
            document.body.appendChild(textDiv);
            _context.next = 8;
            return new Promise(function (resolve) {
              return (0, _reactDom.render)(node, textDiv, resolve);
            });

          case 8:
            textContent = textDiv.textContent;
            (0, _reactDom.unmountComponentAtNode)(textDiv);

            if (textDiv.parentNode) {
              textDiv.parentNode.removeChild(textDiv);
            }

            return _context.abrupt("return", textContent || '');

          case 12:
            return _context.abrupt("return", (0, _toString["default"])(node));

          case 13:
            return _context.abrupt("return", '');

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _getReactNodeText.apply(this, arguments);
}
//# sourceMappingURL=getReactNodeText.js.map
