'use strict';

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

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 _reactFontawesome = require('react-fontawesome');

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

require('./Console.css');

var _helpers = require('../CodeTester/helpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 hasErrors = function hasErrors(logs) {
  return _lodash2.default.some(logs, { type: 'error' });
};

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

  function Console(props) {
    _classCallCheck(this, Console);

    var _this = _possibleConstructorReturn(this, (Console.__proto__ || Object.getPrototypeOf(Console)).call(this, props));

    _this.handleErrors = _this.handleErrors.bind(_this);
    _this.handleKeyUp = _this.handleKeyUp.bind(_this);
    _this.renderLogs = _this.renderLogs.bind(_this);
    return _this;
  }

  _createClass(Console, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (this.props.logs.length === nextProps.logs.length) return;
      this.handleErrors(nextProps);
    }
  }, {
    key: 'handleErrors',
    value: function handleErrors(_ref) {
      var onError = _ref.onError,
          logs = _ref.logs,
          show = _ref.show;

      !show && hasErrors(logs) && onError();
    }
  }, {
    key: 'handleKeyUp',
    value: function handleKeyUp(e) {
      // esc key
      if (e.keyCode === 27) {
        this.props.onToggleConsole();
      }
    }
  }, {
    key: 'renderLogs',
    value: function renderLogs(logs) {
      return logs.map(function (_ref2, i) {
        var log = _ref2.log,
            type = _ref2.type;
        return _react2.default.createElement(
          'div',
          { key: 'div-' + i },
          _react2.default.createElement(
            'pre',
            { className: 'console-' + type, key: 'pre-' + i },
            type === 'error' && 'Error: ',
            log
          )
        );
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          logs = _props.logs,
          show = _props.show,
          onToggleConsole = _props.onToggleConsole,
          style = _props.style;

      return _react2.default.createElement(
        'div',
        { className: 'console-container', style: style },
        _react2.default.createElement(
          'div',
          {
            className: 'console-close',
            id: 'console-close',
            onClick: onToggleConsole,
            role: 'button',
            tabIndex: 0,
            onKeyUp: this.handleKeyUp
          },
          _react2.default.createElement(_reactFontawesome2.default, {
            name: show ? 'toggle-right' : 'toggle-left',
            className: show ? 'toggle-right' : 'toggle-left'
          })
        ),
        _react2.default.createElement(
          'div',
          { className: 'console-title' },
          'Console'
        ),
        _react2.default.createElement(
          'div',
          { className: 'console-logs' },
          this.renderLogs(logs)
        )
      );
    }
  }]);

  return Console;
}(_react.Component);

Console.propTypes = {
  // eslint-disable-next-line
  style: _propTypes2.default.object,
  logs: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    log: _propTypes2.default.string,
    type: _propTypes2.default.string
  })),
  show: _propTypes2.default.bool,
  onToggleConsole: _propTypes2.default.func,
  // eslint-disable-next-line
  onError: _propTypes2.default.func
};

Console.defaultProps = {
  style: {},
  logs: [],
  show: false,
  onToggleConsole: _helpers.defaultFunc,
  onError: _helpers.defaultFunc
};

exports.default = Console;