import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _isEqual from "lodash/isEqual";
import React, { useEffect } from 'react';
import { useStore } from 'react-redux';
import { Editor } from "./Editor";
import { connect } from "../redux";
import { ifCompare } from "../../utils/util";
import "./formBuilder.less";
import "./formBuilder.css";
import "./formApp.css";

var FormBuilder = function FormBuilder(props) {
  var save = props.save,
      _props$load = props.load,
      load = _props$load === void 0 ? {} : _props$load,
      reset = props.reset,
      setInitialPageJson = props.setInitialPageJson,
      updateEvents = props.updateEvents,
      updateMacro = props.updateMacro,
      macro = props.macro,
      resetFlag = props.resetFlag,
      _props$extraPlugins = props.extraPlugins,
      extraPlugins = _props$extraPlugins === void 0 ? [] : _props$extraPlugins,
      afterRegister = props.afterRegister,
      baseContent = props.content,
      restProps = _objectWithoutProperties(props, ["save", "load", "reset", "setInitialPageJson", "updateEvents", "updateMacro", "macro", "resetFlag", "extraPlugins", "afterRegister", "content"]);

  var store = useStore();
  var loadPage = load.loadPage,
      initialData = load.initialData,
      events = load.events;
  useEffect(function () {
    if (macro && macro.length > 0) {
      updateMacro(macro);
    }

    return function () {
      // componentWillUnmount
      reset();
    };
  }, []);
  useEffect(function () {
    if (resetFlag) {
      reset();
    }

    try {
      if (initialData || loadPage && loadPage !== '{}') {
        var content = JSON.parse(loadPage);

        if (content.id && !ifCompare(content, baseContent)) {
          setInitialPageJson({
            content: content,
            initialData: initialData
          });
        } else {
          reset();
        }
      }
    } catch (e) {
      console.error("\u521D\u59CB\u6570\u636E\u89E3\u6790\u9519\u8BEF: ".concat(loadPage, " \u89E3\u6790\u6709\u8BEF\uFF0C\u8BF7\u68C0\u67E5"), e);
    }
  }, [loadPage, initialData, resetFlag]);
  useEffect(function () {
    if (events) {
      updateEvents({
        events: events
      });
    }
  }, [events]);
  return /*#__PURE__*/React.createElement(Editor, Object.assign({
    save: save,
    load: load
  }, restProps));
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    setInitialPageJson: function setInitialPageJson(payload) {
      dispatch({
        type: 'appBuilder/setInitialPageJson',
        payload: payload
      });
    },
    updateEvents: function updateEvents(payload) {
      dispatch({
        type: 'appRender/updateEvents',
        payload: payload
      });
    },
    updateMacro: function updateMacro(payload) {
      dispatch({
        type: 'appRender/updateMacro',
        payload: payload
      });
    },
    reset: function reset() {
      dispatch({
        type: 'appBuilder/reset'
      });
    }
  };
};

export default connect(function (state) {
  var _state$appBuilder, _state$appBuilder2, _state$appBuilder2$pa;

  return {
    resetFlag: (_state$appBuilder = state.appBuilder) === null || _state$appBuilder === void 0 ? void 0 : _state$appBuilder.resetFlag,
    content: ((_state$appBuilder2 = state.appBuilder) === null || _state$appBuilder2 === void 0 ? void 0 : (_state$appBuilder2$pa = _state$appBuilder2.page) === null || _state$appBuilder2$pa === void 0 ? void 0 : _state$appBuilder2$pa.content) || {}
  };
}, mapDispatchToProps, null, {
  areStatePropsEqual: _isEqual
})(FormBuilder);
//# sourceMappingURL=BuildForm.js.map