'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 _reactCodemirror = require('react-codemirror2');

var _reactCodemirror2 = _interopRequireDefault(_reactCodemirror);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

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

require('react-codemirror2/node_modules/codemirror/lib/codemirror.css');

require('react-codemirror2/node_modules/codemirror/mode/javascript/javascript.js');

require('./Editor.css');

require('react-codemirror2/node_modules/codemirror/mode/xml/xml.js');

require('react-codemirror2/node_modules/codemirror/theme/material.css');

require('react-codemirror2/node_modules/codemirror/mode/markdown/markdown.js');

require('react-codemirror2/node_modules/codemirror/addon/lint/lint.js');

require('./codeMirror/addon/lint/eslint/eslint-lint.js');

require('react-codemirror2/node_modules/codemirror/addon/comment/comment.js');

require('react-codemirror2/node_modules/codemirror/addon/edit/closebrackets.js');

require('react-codemirror2/node_modules/codemirror/keymap/sublime.js');

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 import/no-extraneous-dependencies */


/******** File extensions necessary *********/
// Order Important


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

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

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

    _this.state = {
      code: _this.props.code
    };
    _this.handleReset = _this.handleReset.bind(_this);
    _this.updateCode = _this.updateCode.bind(_this);
    return _this;
  }

  // Using CWM because options must be defined before component mounts


  _createClass(Editor, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var onValidCode = this.props.onValidCode;

      var handleValidCode = function handleValidCode(annotationsNotSorted, annotations, cmInstance) {
        var cleanCode = cmInstance.getValue();
        onValidCode(cleanCode);
      };
      this.options = (0, _helpers.getOptions)({}, _lodash2.default.debounce(handleValidCode, 300));
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(_ref) {
      var newCode = _ref.code;
      var prevCode = this.props.code;

      if (prevCode !== newCode) {
        this.setState({ code: newCode });
      }
    }
  }, {
    key: 'handleReset',
    value: function handleReset() {
      this.props.handleReset();
      this.setState({ code: this.props.code });
    }

    // Wrapper to minimize breaking onChange API change on move to react-codemirror2

  }, {
    key: 'updateCode',
    value: function updateCode(editor, metadata, newCode) {
      var currCode = this.state.code;

      if (newCode !== currCode) {
        this.props.onAnyCode(newCode);
      }
      this.setState({ code: newCode });
    }
  }, {
    key: 'render',
    value: function render() {
      var showLints = this.props.showLints;
      var code = this.state.code;

      if (showLints) {
        require('react-codemirror2/node_modules/codemirror/addon/lint/lint.css');
      }

      return _react2.default.createElement(
        'div',
        { id: 'code-tester-editor' },
        _react2.default.createElement(_reactCodemirror2.default, {
          value: code,
          className: 'ReactCodeMirror',
          onChange: this.updateCode,
          options: this.options
        })
      );
    }
  }]);

  return Editor;
}(_react.Component);

Editor.propTypes = {
  code: _propTypes2.default.string,
  handleReset: _propTypes2.default.func,
  onValidCode: _propTypes2.default.func,
  onAnyCode: _propTypes2.default.func,
  showLints: _propTypes2.default.bool,
  hideButtons: _propTypes2.default.bool
};

Editor.defaultProps = {
  code: '// Your code here:',
  hideButtons: false,
  showLints: true,
  onValidCode: function onValidCode(lintedCode) {}, // eslint-disable-line no-unused-vars
  onAnyCode: function onAnyCode(code) {}, // eslint-disable-line no-unused-vars
  handleReset: function handleReset() {
    console.warn('No handleRest function provided.');
  }
};

exports.default = Editor;