'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _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; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _FakeCodeTypingLine = require('./FakeCodeTypingLine');

var _FakeCodeTypingLine2 = _interopRequireDefault(_FakeCodeTypingLine);

var _server = require('react-dom/server');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint-disable react/no-array-index-key */

var FakeCodeTyping = function (_Component) {
  _inherits(FakeCodeTyping, _Component);

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

    return _possibleConstructorReturn(this, (FakeCodeTyping.__proto__ || Object.getPrototypeOf(FakeCodeTyping)).apply(this, arguments));
  }

  _createClass(FakeCodeTyping, [{
    key: 'stripHtml',
    value: function stripHtml(html) {
      // Create a new div element
      var temporalDivElement = document.createElement('div');
      // Set the HTML content with the providen
      temporalDivElement.innerHTML = html;
      // Retrieve the text property of the element (cross-browser support)
      return temporalDivElement.textContent || temporalDivElement.innerText || '';
    }
  }, {
    key: 'fixMultiLineStyle',
    value: function fixMultiLineStyle(html) {}
  }, {
    key: 'parseStyles',
    value: function parseStyles(styles) {
      return styles.split(';').filter(function (style) {
        return style.split(':')[0] && style.split(':')[1];
      }).map(function (style) {
        return [style.split(':')[0].trim().replace(/-./g, function (c) {
          return c.substr(1).toUpperCase();
        }), style.split(':')[1].trim()];
      }).reduce(function (styleObj, style) {
        return _extends({}, styleObj, _defineProperty({}, style[0], style[1]));
      }, {});
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          children = _props.children,
          speed = _props.speed;
      var className = this.props.className;

      // Split code by lines

      var lines = (0, _server.renderToString)(children).split('\n');

      // Remove pre and code tags and get pre style
      var regexOpenTags = /(<pre (style="(.*?)")?.*?>(<code>)?)/;
      var regexCloseTags = /((<\/code>)?<\/pre>)/;

      var openTagsInfo = lines[0].match(regexOpenTags);
      var openTagsStyle = '';

      if (typeof openTagsInfo[3] !== 'undefined') {
        openTagsStyle = openTagsInfo[3];
      }

      lines[0] = lines[0].replace(regexOpenTags, '');
      lines[lines.length - 1] = lines[lines.length - 1].replace(regexCloseTags, '');

      className += ' fake-code-typing';

      var delay = 0;
      var duration = 0;
      var style = {};
      var lineInfo2 = void 0;
      var lineLen = lines.length;

      return _react2.default.createElement(
        'pre',
        { style: this.parseStyles(openTagsStyle) },
        _react2.default.createElement(
          'code',
          null,
          _react2.default.createElement(
            'div',
            { className: className.trim() },
            lines.map(function (line, i) {
              var count = _this2.stripHtml(line).length;
              delay += duration;
              duration = count / speed;

              if (lineInfo2) {
                style = {};
              }

              var lineInfo = line.match(/<span (style="(.*?)")?[^>]*>[^<]*(?!<\/span>)$/);
              lineInfo2 = line.match(/^[^<]*<\/span>$/);

              if (lineInfo) {
                style = _this2.parseStyles(lineInfo[2]);
              }

              return _react2.default.createElement(
                'div',
                { key: i, style: style },
                _react2.default.createElement(
                  _FakeCodeTypingLine2.default,
                  {
                    keepBorder: lineLen === i + 1,
                    count: count,
                    duration: duration,
                    delay: delay
                  },
                  line
                ),
                '\n'
              );
            })
          )
        )
      );
    }
  }]);

  return FakeCodeTyping;
}(_react.Component);

FakeCodeTyping.propTypes = {
  children: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.node), _propTypes2.default.node]).isRequired,
  className: _propTypes2.default.string,
  speed: _propTypes2.default.number
};

FakeCodeTyping.defaultProps = {
  speed: 12,
  className: ''
};

exports.default = FakeCodeTyping;