'use strict';

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

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 _class, _desc, _value, _class2, _descriptor, _descriptor2;

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _mobx = require('mobx');

var _mobxReact = require('mobx-react');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _tooltip = require('../tooltip/tooltip');

var _tooltip2 = _interopRequireDefault(_tooltip);

var _appState = require('../lib/app-state');

var _appState2 = _interopRequireDefault(_appState);

var _events = require('../lib/events');

var _events2 = _interopRequireDefault(_events);

var _util = require('../lib/util');

var _runnablesStore = require('../runnables/runnables-store');

var _runnablesStore2 = _interopRequireDefault(_runnablesStore);

var _scroller = require('../lib/scroller');

var _scroller2 = _interopRequireDefault(_scroller);

var _hooks = require('../hooks/hooks');

var _hooks2 = _interopRequireDefault(_hooks);

var _agents = require('../agents/agents');

var _agents2 = _interopRequireDefault(_agents);

var _routes = require('../routes/routes');

var _routes2 = _interopRequireDefault(_routes);

var _flashOnClick = require('../lib/flash-on-click');

var _flashOnClick2 = _interopRequireDefault(_flashOnClick);

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

function _initDefineProp(target, property, descriptor, context) {
  if (!descriptor) return;
  Object.defineProperty(target, property, {
    enumerable: descriptor.enumerable,
    configurable: descriptor.configurable,
    writable: descriptor.writable,
    value: descriptor.initializer ? descriptor.initializer.call(context) : void 0
  });
}

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

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

function _initializerWarningHelper(descriptor, context) {
  throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');
}

var NoCommands = (0, _mobxReact.observer)(function () {
  return _react2.default.createElement(
    'ul',
    { className: 'hooks-container' },
    _react2.default.createElement(
      'li',
      { className: 'no-commands' },
      'No commands were issued in this test.'
    )
  );
});

var Test = (0, _mobxReact.observer)(_class = (_class2 = function (_Component) {
  _inherits(Test, _Component);

  function Test() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Test);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this].concat(args))), _this), _initDefineProp(_this, 'isOpen', _descriptor, _this), _initDefineProp(_this, '_toggleOpen', _descriptor2, _this), _this._onErrorClick = function (e) {
      e.stopPropagation();
      _this.props.events.emit('show:error', _this.props.model.id);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Test, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._scrollIntoView();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      this._scrollIntoView();
    }
  }, {
    key: '_scrollIntoView',
    value: function _scrollIntoView() {
      var _props = this.props;
      var appState = _props.appState;
      var model = _props.model;
      var scroller = _props.scroller;
      var isActive = model.isActive;
      var shouldRender = model.shouldRender;


      if (appState.autoScrollingEnabled && appState.isRunning && shouldRender && isActive != null) {
        scroller.scrollIntoView(this.refs.container);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var model = this.props.model;


      if (!model.shouldRender) return null;

      return _react2.default.createElement(
        'div',
        {
          ref: 'container',
          className: (0, _classnames2.default)('runnable-wrapper', { 'is-open': this._shouldBeOpen() }),
          onClick: this._toggleOpen,
          style: { paddingLeft: (0, _util.indent)(model.level) }
        },
        _react2.default.createElement(
          'div',
          { className: 'runnable-content-region' },
          _react2.default.createElement('i', { className: 'runnable-state fa' }),
          _react2.default.createElement(
            'span',
            { className: 'runnable-title' },
            model.title
          ),
          _react2.default.createElement(
            'div',
            { className: 'runnable-controls' },
            _react2.default.createElement(
              _tooltip2.default,
              { placement: 'top', title: 'One or more commands failed' },
              _react2.default.createElement('i', { className: 'fa fa-warning' })
            )
          )
        ),
        this._contents(),
        _react2.default.createElement(
          _flashOnClick2.default,
          {
            message: 'Printed output to your console!',
            onClick: this._onErrorClick
          },
          _react2.default.createElement(
            'pre',
            { className: 'test-error' },
            model.err.displayMessage
          )
        )
      );
    }
  }, {
    key: '_contents',
    value: function _contents() {
      // performance optimization - don't render contents if not open
      if (!this._shouldBeOpen()) return null;

      var model = this.props.model;


      return _react2.default.createElement(
        'div',
        {
          className: 'runnable-instruments collapsible-content',
          onClick: function onClick(e) {
            e.stopPropagation();
          }
        },
        _react2.default.createElement(_agents2.default, { model: model }),
        _react2.default.createElement(_routes2.default, { model: model }),
        _react2.default.createElement(
          'div',
          { className: 'runnable-commands-region' },
          model.commands.length ? _react2.default.createElement(_hooks2.default, { model: model }) : _react2.default.createElement(NoCommands, null)
        )
      );
    }
  }, {
    key: '_shouldBeOpen',
    value: function _shouldBeOpen() {
      // if this.isOpen is non-null, prefer that since the user has
      // explicity chosen to open or close the test
      if (this.isOpen !== null) return this.isOpen;

      // otherwise, look at reasons to auto-open the test
      return this.props.model.state === 'failed' || this.props.model.isLongRunning || this.props.runnablesStore.hasSingleTest;
    }
  }]);

  return Test;
}(_react.Component), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'isOpen', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return null;
  }
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, '_toggleOpen', [_mobx.action], {
  enumerable: true,
  initializer: function initializer() {
    var _this2 = this;

    return function () {
      if (_this2.isOpen === null) {
        _this2.isOpen = !_this2._shouldBeOpen();
      } else {
        _this2.isOpen = !_this2.isOpen;
      }
    };
  }
})), _class2)) || _class;

Test.defaultProps = {
  appState: _appState2.default,
  events: _events2.default,
  runnablesStore: _runnablesStore2.default,
  scroller: _scroller2.default
};

exports.NoCommands = NoCommands;
exports.default = Test;