'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _transformimeReact = require('transformime-react');

var _richestMime = require('./richest-mime');

var _richestMime2 = _interopRequireDefault(_richestMime);

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

const Ansi = require('ansi-to-react');

function Output(props) {
  const output = props.output;
  const outputType = output.get('output_type');
  switch (outputType) {
    case 'execute_result':
    // We can defer to display data here, the cell number will be handled
    // separately. For reference, it is output.get('execution_count')
    // The execution_count belongs in the component above if
    // this is a code cell

    // falls through
    case 'display_data':
      {
        const bundle = output.get('data');
        const metadata = output.get('metadata');
        return _react2.default.createElement(_richestMime2.default, {
          bundle: bundle,
          metadata: metadata,
          displayOrder: props.displayOrder,
          transforms: props.transforms,
          theme: props.theme
        });
      }
    case 'stream':
      {
        const text = output.get('text');
        switch (output.get('name')) {
          case 'stdout':
          case 'stderr':
            return _react2.default.createElement(
              Ansi,
              null,
              text
            );
          default:
            return null;
        }
      }
    case 'error':
      {
        const traceback = output.get('traceback');
        if (!traceback) {
          return _react2.default.createElement(
            Ansi,
            null,
            `${ output.get('ename') }: ${ output.get('evalue') }`
          );
        }
        return _react2.default.createElement(
          Ansi,
          null,
          traceback.join('\n')
        );
      }
    default:
      return null;
  }
}

Output.propTypes = {
  displayOrder: _react2.default.PropTypes.instanceOf(_immutable2.default.List),
  output: _react2.default.PropTypes.any,
  transforms: _react2.default.PropTypes.instanceOf(_immutable2.default.Map),
  theme: _react2.default.PropTypes.string
};

Output.defaultProps = {
  transforms: _transformimeReact.transforms,
  displayOrder: _transformimeReact.displayOrder
};