text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  
  var _host = require('../vdom/host');
  
  var _host2 = _interopRequireDefault(_host);
  
  var _getComponentTree = require('./getComponentTree');
  
  var _getComponentTree2 = _interopRequireDefault(_getComponentTree);
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  var INDENTATION_SIZE = 2;
  var MAX_DEPTH = 2;
  var MAX_STRING_LENGTH = 50;
  
  /**
   * Dump all Native root views and their content. This function tries
   * it best to get the content but ultimately relies on implementation details
   * of  and will fail in future versions.
   */
  function dumpTree() {
    try {
      return getDumpTree();
    } catch (e) {
      return 'Failed to dump tree: ' + e;
    }
  }
  
  function getDumpTree() {
    var output = '';
    var rootIds = Object.getOwnPropertyNames(_host2.default.rootComponents);
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;
  
    try {
      for (var _iterator = rootIds[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var rootId = _step.value;
  
        var inst = _host2.default.rootComponents[rootId];
        output += '============ Root ID: ' + rootId + ' ============\n';
        output += dumpNode(inst.getRenderedComponent(), 0);
        output += '============ End root ID: ' + rootId + ' ============\n';
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }
  
    return output;
  }
  
  function dumpNode(node, identation) {
    var data = (0, _getComponentTree2.default)(node);
    if (data.nodeType === 'Text') {
      return indent(identation) + data.text + '\n';
    } else if (data.nodeType === 'Empty') {
      return '';
    }
    var output = indent(identation) + ('<' + data.name);
    if (data.nodeType === 'Composite') {
      var _iteratorNormalCompletion2 = true;
      var _didIteratorError2 = false;
      var _iteratorError2 = undefined;
  
      try {
        for (var _iterator2 = Object.getOwnPropertyNames(data.props || {})[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
          var propName = _step2.value;
  
          if (isNormalProp(propName)) {
            try {
              var value = convertValue(data.props[propName]);
              if (value) {
                output += ' ' + propName + '=' + value;
              }
            } catch (e) {
              var message = '[Failed to get property: ' + e + ']';
              output += ' ' + propName + '=' + message;
            }
          }
        }
      } catch (err) {
        _didIteratorError2 = true;
        _iteratorError2 = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion2 && _iterator2.return) {
            _iterator2.return();
          }
        } finally {
          if (_didIteratorError2) {
            throw _iteratorError2;
          }
        }
      }
    }
    var childOutput = '';
    var _iteratorNormalCompletion3 = true;
    var _didIteratorError3 = false;
    var _iteratorError3 = undefined;
  
    try {
      for (var _iterator3 = (data.children || [])[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
        var child = _step3.value;
  
        childOutput += dumpNode(child, identation + 1);
      }
    } catch (err) {
      _didIteratorError3 = true;
      _iteratorError3 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion3 && _iterator3.return) {
          _iterator3.return();
        }
      } finally {
        if (_didIteratorError3) {
          throw _iteratorError3;
        }
      }
    }
  
    if (childOutput) {
      output += '>\n' + childOutput + indent(identation) + ('</' + data.name + '>\n');
    } else {
      output += ' />\n';
    }
  
    return output;
  }
  
  function isNormalProp(name) {
    switch (name) {
      case 'children':
      case 'key':
      case 'ref':
        return false;
      default:
        return true;
    }
  }
  
  function convertObject(object, depth) {
    if (depth >= MAX_DEPTH) {
      return '[...omitted]';
    }
    var output = '{';
    var first = true;
    var _iteratorNormalCompletion4 = true;
    var _didIteratorError4 = false;
    var _iteratorError4 = undefined;
  
    try {
      for (var _iterator4 = Object.getOwnPropertyNames(object)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
        var key = _step4.value;
  
        if (!first) {
          output += ', ';
        }
        // $FlowFixMe(>=0.28.0)
        output += key + ': ' + convertValue(object[key], depth + 1);
        first = false;
      }
    } catch (err) {
      _didIteratorError4 = true;
      _iteratorError4 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion4 && _iterator4.return) {
          _iterator4.return();
        }
      } finally {
        if (_didIteratorError4) {
          throw _iteratorError4;
        }
      }
    }
  
    return output + '}';
  }
  
  function convertValue(value) {
    var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  
    if (!value) {
      return null;
    }
  
    switch (typeof value === 'undefined' ? 'undefined' : _typeof(value)) {
      case 'string':
        return JSON.stringify(possiblyEllipsis(value).replace('\n', '\\n'));
      case 'boolean':
      case 'number':
        return JSON.stringify(value);
      case 'function':
        return '[function]';
      case 'object':
        return convertObject(value, depth);
      default:
        return null;
    }
  }
  
  function possiblyEllipsis(value) {
    if (value.length > MAX_STRING_LENGTH) {
      return value.slice(0, MAX_STRING_LENGTH) + '...';
    } else {
      return value;
    }
  }
  
  function indent(size) {
    return ' '.repeat(size * INDENTATION_SIZE);
  }
  
  exports.default = dumpTree;