"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 _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
// Import a few CodeMirror themes; these are used to match alternative

// bootstrap ones.


var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _reactCodemirror = require("react-codemirror");

var _reactCodemirror2 = _interopRequireDefault(_reactCodemirror);

require("codemirror/mode/javascript/javascript");

require("codemirror/mode/xml/xml");

require("codemirror/mode/markdown/markdown");

require("codemirror/lib/codemirror.css");

var _Error = require("./Error");

var _Error2 = _interopRequireDefault(_Error);

var _utils = require("../utils");

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

// Patching CodeMirror#componentWillReceiveProps so it's executed synchronously
// Ref https://github.com/mozilla-services/react-jsonschema-form/issues/174
_reactCodemirror2.default.prototype.componentWillReceiveProps = function (nextProps) {
  if (this.codeMirror && nextProps.value !== undefined && this.codeMirror.getValue() != nextProps.value) {
    this.codeMirror.setValue(nextProps.value);
  }
  if (_typeof(nextProps.options) === "object") {
    for (var optionName in nextProps.options) {
      if (nextProps.options.hasOwnProperty(optionName)) {
        this.codeMirror.setOption(optionName, nextProps.options[optionName]);
      }
    }
  }
};

var fromJson = function fromJson(json) {
  return JSON.parse(json);
};

var jsonOptions = {
  theme: "default",
  height: "auto",
  viewportMargin: Infinity,
  mode: {
    name: "javascript",
    json: true,
    statementIndent: 2
  },
  lineNumbers: true,
  lineWrapping: true,
  indentWithTabs: false,
  tabSize: 2
};

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

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

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

    _this.onCodeChange = function (code) {
      _this.setState({ valid: true, code: code, loading: true });
      clearTimeout(_this.timeout);
      _this.timeout = setTimeout(function () {
        _this.setState({ loading: false });
        try {
          _this.props.onChange(fromJson(_this.state.code));
        } catch (error) {
          _this.setState({ valid: false, code: code, error: error });
        }
      }, 750);
    };

    _this.state = { valid: true, code: props.code };
    return _this;
  }

  _createClass(JsonEditor, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      this.setState({ valid: true, code: props.code });
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      return (0, _utils.shouldRender)(this, nextProps, nextState);
    }
  }, {
    key: "render",
    value: function render() {
      var title = this.props.title;
      var _state = this.state,
          valid = _state.valid,
          loading = _state.loading;

      var icon = loading ? "refresh" : valid ? "ok" : "remove";
      return _react2.default.createElement(
        "div",
        null,
        !this.state.valid && _react2.default.createElement(_Error2.default, { error: this.state.error }),
        _react2.default.createElement(
          "div",
          { className: "panel panel-default" },
          _react2.default.createElement(
            "div",
            { className: "panel-heading" },
            _react2.default.createElement("span", { className: "glyphicon glyphicon-" + icon }),
            _react2.default.createElement(
              "span",
              null,
              " ",
              title
            )
          ),
          _react2.default.createElement(_reactCodemirror2.default, {
            value: this.state.code,
            onChange: this.onCodeChange,
            options: jsonOptions
          })
        )
      );
    }
  }]);

  return JsonEditor;
}(_react.Component);

exports.default = JsonEditor;