import _isEqual from "lodash/isEqual";
import React, { useEffect } from "react";
import { connect } from "../../editor/redux";
import { EditorPluginsLoader } from "../../editor/components/PluginsLoader/PluginsLoader";
import Element from "./Element";
import { getContent } from "../selectors"; // import { compatOldJson } from '@whalecloud/page-builder/utils/compatOldJson';

var RenderForm = function RenderForm(props) {
  var pageContent = props.pageContent,
      _props$content = props.content,
      content = _props$content === void 0 ? {} : _props$content,
      setInitialPageJson = props.setInitialPageJson,
      nameSpace = props.nameSpace,
      afterRegister = props.afterRegister; // updateLoadPage

  useEffect(function () {
    // 加载初始化数据
    if (pageContent && pageContent !== '{}') {
      try {
        var _content = JSON.parse(pageContent); // 兼容旧数据
        // const newContent = compatOldJson(content);


        setInitialPageJson({
          content: _content
        }, nameSpace);
      } catch (e) {
        console.error("\u521D\u59CB\u6570\u636E\u89E3\u6790\u9519\u8BEF: ".concat(pageContent, " \u89E3\u6790\u6709\u8BEF\uFF0C\u8BF7\u68C0\u67E5"), e);
      }
    }
  }, [pageContent]);
  return /*#__PURE__*/React.createElement(EditorPluginsLoader, {
    type: "render",
    afterRegister: afterRegister
  }, content && content.elements && content.elements.length > 0 ? /*#__PURE__*/React.createElement(Element, {
    element: content,
    nameSpace: nameSpace
  }) : '');
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    setInitialPageJson: function setInitialPageJson(payload, nameSpace) {
      dispatch({
        type: "".concat(nameSpace, "/setInitialPageJson"),
        payload: payload
      });
    }
  };
};

export default connect(function (state, props) {
  var _props$nameSpace = props.nameSpace,
      nameSpace = _props$nameSpace === void 0 ? 'appRender' : _props$nameSpace;
  return {
    content: state[nameSpace] ? getContent(state, nameSpace) : null,
    nameSpace: nameSpace
  };
}, mapDispatchToProps, null, {
  areStatePropsEqual: _isEqual
})(RenderForm);
//# sourceMappingURL=RenderPage.js.map