"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 = MarkdownViewer;
var _react = _interopRequireWildcard(require("react"));
var _loading = _interopRequireDefault(require("../containers/loading"));
var _useMathjax = _interopRequireDefault(require("../hooks/use-mathjax"));
var _slateViewer = _interopRequireDefault(require("../editors/slate-viewer"));
var _slateConvert = require("../slate-convert");
function MarkdownViewer(_ref) {
  let {
    isFetching,
    value,
    mathJaxSource,
    isShowOutline,
    scrollRef,
    onLinkClick
  } = _ref;
  const [richValue, setRichValue] = (0, _react.useState)([]);
  const [isLoading, setIsLoading] = (0, _react.useState)(true);
  const {
    isLoadingMathJax
  } = (0, _useMathjax.default)(mathJaxSource);
  (0, _react.useEffect)(() => {
    if (!isFetching) {
      setIsLoading(true);
      const richValue = value ? JSON.parse(value) : (0, _slateConvert.mdStringToSlate)('');
      setRichValue(richValue);
      setTimeout(() => {
        setIsLoading(false);
      }, 0);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isFetching, value]);
  const props = {
    isSupportFormula: !!mathJaxSource,
    value: richValue,
    isShowOutline: isShowOutline,
    scrollRef: scrollRef,
    onLinkClick: onLinkClick
  };
  if (isFetching || isLoading || isLoadingMathJax) {
    return /*#__PURE__*/_react.default.createElement(_loading.default, null);
  }
  return /*#__PURE__*/_react.default.createElement(_slateViewer.default, props);
}