"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

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

var MonacoEditor = _interopRequireWildcard(require("monaco-editor"));

var _ahooks = require("ahooks");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var ScriptEditor = function ScriptEditor(_ref) {
  var language = _ref.language,
      prevValue = _ref.prevValue,
      readOnly = _ref.readOnly,
      theme = _ref.theme,
      onChange = _ref.onChange,
      className = _ref.className,
      options = _ref.options;
  var ref = (0, _react.useRef)();
  var editorRef = (0, _react.useRef)();
  var onChangeSubscribeRef = (0, _react.useRef)();

  var init = function init() {
    editorRef.current = MonacoEditor.editor.create(ref.current, (0, _extends2["default"])({
      value: prevValue,
      language: language,
      contextmenu: false,
      minimap: {
        enabled: false
      },
      readOnly: readOnly,
      theme: theme
    }, options));
    onChangeSubscribeRef.current = editorRef.current.onDidChangeModelContent(function () {
      onChange && onChange(editorRef.current.getValue());
    });
  };

  var destroy = function destroy() {
    if (editorRef.current) {
      //使用完成销毁实例
      editorRef.current.dispose();
      var model = editorRef.current.getModel();

      if (model) {
        model.dispose();
      }
    }

    if (onChangeSubscribeRef.current) {
      debugger;
      onChangeSubscribeRef.current.dispose();
    }
  };

  (0, _react.useEffect)(function () {
    init();
    return destroy;
  }, []);
  (0, _ahooks.useUpdateEffect)(function () {
    MonacoEditor.editor.setModelLanguage(editorRef.current.getModel(), language);
  }, [language]);
  (0, _ahooks.useUpdateEffect)(function () {
    editorRef.current.updateOptions({
      readOnly: readOnly,
      theme: theme
    });
  }, [readOnly, theme]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: ref,
    style: {
      height: '100%'
    },
    className: className
  });
};

var _default = ScriptEditor;
exports["default"] = _default;
ScriptEditor.propTypes = {
  language: _propTypes["default"].string,
  prevValue: _propTypes["default"].string,
  readOnly: _propTypes["default"].bool,
  theme: _propTypes["default"].string,
  onChange: _propTypes["default"].func,
  className: _propTypes["default"].string,
  options: _propTypes["default"].object
};
ScriptEditor.defaultProps = {
  prevValue: '',
  readOnly: false,
  theme: 'vs'
};