'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactCodemirror = require('react-codemirror2');

var _cm = require('./cm.modes');

var _cm2 = require('./cm.options');

var _cm3 = _interopRequireDefault(_cm2);

require('./style.scss');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } /* istanbul ignore file */


var CodeEditor = function CodeEditor(_ref) {
  var className = _ref.className,
      _ref$code = _ref.code,
      code = _ref$code === undefined ? '' : _ref$code,
      lang = _ref.lang,
      _ref$options = _ref.options,
      options = _ref$options === undefined ? {} : _ref$options,
      children = _ref.children,
      attr = _objectWithoutProperties(_ref, ['className', 'code', 'lang', 'options', 'children']);

  var _useState = (0, _react.useState)(children && typeof children === 'string' ? children : code),
      value = _useState[0],
      setValue = _useState[1];

  var _useState2 = (0, _react.useState)((0, _cm.getMode)(lang)),
      mode = _useState2[0],
      setMode = _useState2[1];

  (0, _react.useEffect)(function () {
    var incValue = children && typeof children === 'string' ? children : code;
    setValue(incValue);
  }, [code, children]);

  (0, _react.useEffect)(function () {
    setMode(function (prevMode) {
      var newMode = (0, _cm.getMode)(lang);
      if (newMode !== prevMode) return newMode;
      return prevMode;
    });
  }, [lang]);

  return _react2.default.createElement(
    'div',
    { className: 'CodeEditor ' + (className || '') },
    _react2.default.createElement(_reactCodemirror.Controlled, _extends({}, attr, {
      onBeforeChange: function onBeforeChange(editor, data, updatedCode) {
        return setValue(updatedCode);
      },
      options: _extends({}, _cm3.default, options, { mode: mode }),
      value: value
    }))
  );
};

CodeEditor.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes2.default.string,
  /** Code to show in the editor.
   */
  code: _propTypes2.default.string,
  /** CodeMirror language mode to use.
   */
  lang: _propTypes2.default.string,
  /** Custom CodeMirror  configuration.
   */
  options: _propTypes2.default.object,
  /** Syntax highlighting theme.
   */
  theme: _propTypes2.default.oneOf(['light', 'dark'])
} : {};

CodeEditor.defaultProps = {
  options: {},
  theme: 'dark'
};

exports.default = CodeEditor;
module.exports = exports['default'];