import _isEqual from "lodash/isEqual";
import React, { useState, useEffect } from "react";
import { message } from '@whalecloud/fdx';
import { connect } from "../../editor/redux";
import { EditorPluginsLoader } from "../../editor/components/PluginsLoader/PluginsLoader";
import Element from "./Element";
import { getContent } from "../selectors";
import { compileModule, transpilePlugin } from "../../utils/transformJS";
import { resetFormRender } from "../../editor/utils";

var RenderForm = function RenderForm(props) {
  var load = props.load,
      disabled = props.disabled,
      save = props.save,
      forwardedRef = props.forwardedRef,
      _props$content = props.content,
      content = _props$content === void 0 ? {} : _props$content,
      initData = props.initData,
      macro = props.macro,
      eventJs = props.eventJs,
      setInitialPageJson = props.setInitialPageJson,
      updateEvents = props.updateEvents,
      updateMacro = props.updateMacro,
      nameSpace = props.nameSpace,
      afterRegister = props.afterRegister;
  var loadPage = load.loadPage,
      initialData = load.initialData,
      events = load.events;
  useState(eventJs && eventJs.created());
  var event = eventJs;

  var init = function init() {
    // componentDidMount
    if (event && event.mounted) {
      event.mounted();
    }
  };

  var cleanUp = function cleanUp() {
    if (event && event.destroyed) {
      event.destroyed();
    }
  };

  useEffect(function () {
    init();
    return function () {
      // componentWillUnmount
      cleanUp();
      resetFormRender(nameSpace);
    };
  }, []); // updateEvents

  useEffect(function () {
    if (events) {
      try {
        var eventJsUpdate = compileModule(transpilePlugin(events)).default;
        updateEvents({
          events: events,
          eventJsUpdate: eventJsUpdate
        }, nameSpace);
        event = eventJsUpdate;
        init();
      } catch (error) {
        message.error('事件解析有误，请检查');
        console.error('事件解析有误，请检查', error);
      }
    }
  }, [events]); // updateMacro

  useEffect(function () {
    // 加载宏数据
    if (macro && macro.length > 0) {
      updateMacro(macro, nameSpace);
    }
  }, [macro]); // updateLoadPage

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


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

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

export default connect(function (state, props) {
  var _state$nameSpace, _state$nameSpace2;

  var _props$nameSpace = props.nameSpace,
      nameSpace = _props$nameSpace === void 0 ? 'appRender' : _props$nameSpace;
  return {
    content: state[nameSpace] ? getContent(state, nameSpace) : null,
    initData: (_state$nameSpace = state[nameSpace]) === null || _state$nameSpace === void 0 ? void 0 : _state$nameSpace.initialData,
    eventJs: (_state$nameSpace2 = state[nameSpace]) === null || _state$nameSpace2 === void 0 ? void 0 : _state$nameSpace2.eventJs,
    nameSpace: nameSpace
  };
}, mapDispatchToProps, null, {
  areStatePropsEqual: _isEqual
})(RenderForm);
//# sourceMappingURL=RenderForm.js.map