"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 _Viewer = require("./Viewer");

var _Viewer2 = _interopRequireDefault(_Viewer);

var _JSEditor = require("./JSEditor");

var _JSEditor2 = _interopRequireDefault(_JSEditor);

var _JSONEditor = require("./JSONEditor");

var _JSONEditor2 = _interopRequireDefault(_JSONEditor);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var toJson = function toJson(val) {
  return JSON.stringify(val, null, 2);
};

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

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

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

    _this.setActive = function (active) {
      return _this.setState({ active: active });
    };

    _this.toView = function (editor) {
      switch (editor.type) {
        case "viewer":
          {
            var title = editor.title,
                source = editor.source;

            return _react2.default.createElement(_Viewer2.default, { title: title, code: toJson(source) });
          }
        case "json":
          {
            var _title = editor.title,
                _source = editor.source,
                onChange = editor.onChange,
                onError = editor.onError;

            return _react2.default.createElement(_JSONEditor2.default, {
              title: _title,
              code: toJson(_source),
              onChange: onChange,
              onError: onError
            });
          }
        case "js":
          {
            var _title2 = editor.title,
                _source2 = editor.source,
                _onChange = editor.onChange,
                _onError = editor.onError;

            return _react2.default.createElement(_JSEditor2.default, {
              title: _title2,
              code: _source2,
              onChange: _onChange,
              onError: _onError
            });
          }
      }
    };

    _this.state = { active: _this.props.editors[0].title };
    return _this;
  }

  _createClass(EditorsPanel, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var editors = this.props.editors;

      var activeTitle = this.state.active;
      var activeEditor = editors.find(function (editor) {
        return editor.title === activeTitle;
      });

      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(
          "ul",
          { className: "nav nav-pills" },
          editors.map(function (_ref) {
            var title = _ref.title;
            return _react2.default.createElement(
              "li",
              {
                key: title,
                className: activeTitle === title ? "active" : "",
                onClick: function onClick() {
                  return _this2.setActive(title);
                } },
              _react2.default.createElement(
                "a",
                null,
                title
              )
            );
          })
        ),
        _react2.default.createElement("br", null),
        this.toView(activeEditor)
      );
    }
  }]);

  return EditorsPanel;
}(_react.Component);

exports.default = EditorsPanel;


EditorsPanel.propTypes = {
  editors: _propTypes2.default.array.isRequired
};