"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _loading = _interopRequireDefault(require("../containers/loading"));
var _slateConvert = require("../slate-convert");
var _useMathjax = _interopRequireDefault(require("../hooks/use-mathjax"));
var _simpleSlateEditor = _interopRequireDefault(require("../editors/simple-slate-editor"));
const SeaTableEditor = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
  let {
    isFetching,
    value,
    editorApi,
    mathJaxSource,
    columns,
    onSave: propsOnSave,
    onContentChanged: propsOnContentChanged
  } = _ref;
  const [richValue, setRichValue] = (0, _react.useState)([]);
  const [isLoading, setIsLoading] = (0, _react.useState)(true);
  const {
    isLoadingMathJax
  } = (0, _useMathjax.default)(mathJaxSource);
  (0, _react.useImperativeHandle)(ref, () => {
    return {
      getValue: () => {
        const mdStringValue = (0, _slateConvert.slateToMdString)(richValue);
        return mdStringValue;
      },
      getSlateValue: () => {
        return richValue;
      }
    };
  }, [richValue]);
  (0, _react.useEffect)(() => {
    if (!isFetching) {
      const richValue = value ? JSON.parse(value) : (0, _slateConvert.mdStringToSlate)('');
      setRichValue(richValue);
      setIsLoading(false);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isFetching]);
  const onContentChanged = (0, _react.useCallback)(content => {
    setRichValue(content);
    propsOnContentChanged && propsOnContentChanged();
  }, [propsOnContentChanged]);
  const props = {
    isSupportFormula: !!mathJaxSource,
    value: richValue,
    editorApi: editorApi,
    columns: columns,
    onSave: propsOnSave,
    onContentChanged: onContentChanged
  };
  if (isFetching || isLoading || isLoadingMathJax) {
    return /*#__PURE__*/_react.default.createElement(_loading.default, null);
  }
  return /*#__PURE__*/_react.default.createElement(_simpleSlateEditor.default, props);
});
var _default = exports.default = SeaTableEditor;