import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";

var _class, _class2, _temp;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import React, { Component, Fragment } from 'react';
import { injectIntl } from 'react-intl';
import classnames from 'classnames';
import { checkFormat, changedValue } from "./utils";
import 'codemirror/addon/merge/merge.css';
import 'codemirror/lib/codemirror.css';
import 'codemirror/addon/lint/lint.css';
import "./theme-chd.css";
import CodeMirror from "./editor/CodeMirror";
import 'codemirror/addon/lint/lint.js';
import "./index.less";
import "./yaml-lint";
import "./yaml-mode";
import "./merge";
var HAS_ERROR = true;
var NO_ERROR = false;

var YamlEditor = injectIntl(_class = (_temp = _class2 = /*#__PURE__*/function (_Component) {
  _inherits(YamlEditor, _Component);

  var _super = _createSuper(YamlEditor);

  function YamlEditor(props) {
    var _this;

    _classCallCheck(this, YamlEditor);

    _this = _super.call(this, props);

    _this.onChange = function (value) {
      var _this$props = _this.props,
          onValueChange = _this$props.onValueChange,
          originValue = _this$props.originValue,
          handleEnableNext = _this$props.handleEnableNext;

      var hasError = _this.checkYamlFormat(value);

      var changed = false;

      if (!hasError) {
        changed = changedValue(originValue, value, function (flag) {
          _this.setState({
            errorTip: flag
          });

          handleEnableNext(flag);
        });
      }

      onValueChange(value, changed);
    };

    _this.state = {
      errorTip: false
    };
    _this.options = {
      // chd 自定制的主题配色
      theme: 'chd',
      mode: 'text/chd-yaml',
      readOnly: props.readOnly,
      lineNumbers: !props.readOnly,
      lineWrapping: true,
      viewportMargin: Infinity,
      lint: !props.readOnly && props.showError,
      gutters: !props.readOnly ? ['CodeMirror-lint-markers'] : []
    };
    return _this;
  }

  _createClass(YamlEditor, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props2 = this.props,
          value = _this$props2.value,
          onValueChange = _this$props2.onValueChange;
      this.checkYamlFormat(value); // 初始化组件时设置值

      onValueChange(value);
    }
  }, {
    key: "checkYamlFormat",
    value:
    /**
     * 校验Yaml格式
     * 校验规则来源 https://github.com/nodeca/js-yaml
     * @param {*} values
     */
    function checkYamlFormat(values) {
      var handleEnableNext = this.props.handleEnableNext;
      var errorTip = NO_ERROR; // yaml 格式校验结果

      var formatResult = checkFormat(values);

      if (formatResult && formatResult.length) {
        errorTip = HAS_ERROR;
        handleEnableNext(HAS_ERROR);
      } else {
        errorTip = NO_ERROR;
        handleEnableNext(NO_ERROR);
      } // 显示编辑器下方的错误 tips


      this.setState({
        errorTip: errorTip
      });
      return errorTip;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      // originValue 用做merge对比的源数据
      var _this$props3 = this.props,
          formatMessage = _this$props3.intl.formatMessage,
          originValue = _this$props3.originValue,
          value = _this$props3.value,
          modeChange = _this$props3.modeChange,
          readOnly = _this$props3.readOnly,
          showError = _this$props3.showError,
          className = _this$props3.className;
      var errorTip = this.state.errorTip;
      var wrapClass = classnames(_defineProperty({
        'c7ncd-yaml-wrapper': true,
        'c7ncd-yaml-readonly': readOnly
      }, className, true));
      return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("div", {
        className: wrapClass
      }, /*#__PURE__*/React.createElement(CodeMirror, {
        modeChange: modeChange,
        options: this.options,
        value: value || '',
        originValue: originValue,
        onChange: this.onChange,
        ref: function ref(instance) {
          _this2.yamlEditor = instance;
        }
      })), showError && errorTip ? /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-yaml-error"
      }, /*#__PURE__*/React.createElement(_Icon, {
        type: "error",
        className: "c7ncd-yaml-error-icon"
      }), /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-yaml-error-msg"
      }, formatMessage({
        id: 'yaml.error.tooltip'
      }))) : null);
    }
  }]);

  return YamlEditor;
}(Component), _class2.defaultProps = {
  readOnly: true,
  originValue: '',
  modeChange: true,
  showError: true,
  handleEnableNext: function handleEnableNext() {},
  onValueChange: function onValueChange() {}
}, _temp)) || _class;

export { YamlEditor as default };