import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { fetch } from '@fishx/rest';
import { set } from 'dot-prop-immutable';
import { message } from '@whalecloud/fdx';
import { getFlattenElements, getNewState } from "../utils/util";
import { concatMacro } from "../utils/macro";
import { initSptContent } from "../utils/initDatas";
import { compileModule, transpilePlugin } from "../utils/transformJS";
import { cssToSetting, loadTheme, matchCodePrefix } from "../editor/utils";

var handleInitContent = function handleInitContent() {
  var element = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var disabled = arguments.length > 1 ? arguments[1] : undefined;
  var _element$elements = element.elements,
      elements = _element$elements === void 0 ? [] : _element$elements;

  if (elements.length > 0) {
    elements.forEach(function (ele, index) {
      // 当时表格类型组件时，将disabled属性设置在tableSettings上
      if (ele.type && ele.type === 'tableEdit') {
        ele = set(ele, 'data.componentSettings.disabled', disabled);
        element.elements[index] = ele;
      } else if (ele.data && ele.data.formItemSettings) {
        ele = set(ele, 'data.formItemSettings.disabled', disabled);
        element.elements[index] = ele;
      } else {
        handleInitContent(ele, disabled);
      }
    });
  }
}; // 动态注册namespace


export default (function () {
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'appRender';
  return {
    namespace: name,
    state: {
      elements: {},
      page: {},
      initialData: {},
      previewUrl: "/page-builder/pages",
      macro: concatMacro(),
      // 宏数据
      form: null,
      // 表单实例
      fetch: fetch,
      // fetch对象 用于请求数据
      events: initSptContent // 事件对象js字符串

    },
    effects: {
      setInitialPageJson: /*#__PURE__*/_regeneratorRuntime.mark(function setInitialPageJson(_ref, _ref2) {
        var payload, put, select, content, _payload$initialData, initialData, disabled, deviceType, currentPage, _cssToSetting, _cssToSetting$tempora, temporary, _cssToSetting$custom, custom, _cssToSetting$page, page, _content$theme, _name, url;

        return _regeneratorRuntime.wrap(function setInitialPageJson$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                payload = _ref.payload;
                put = _ref2.put, select = _ref2.select;
                content = payload.content, _payload$initialData = payload.initialData, initialData = _payload$initialData === void 0 ? {} : _payload$initialData, disabled = payload.disabled; // preview type: pc/mobile/pad

                _context.next = 5;
                return select(function (state) {
                  return state.ui.previewDevice;
                });

              case 5:
                deviceType = _context.sent;
                content.data['className'] = matchCodePrefix[deviceType];

                if (typeof disabled === 'boolean' && content) {
                  handleInitContent(content, disabled);
                }

                _context.next = 10;
                return put({
                  type: 'save',
                  payload: {
                    page: {
                      content: content
                    },
                    initialData: initialData
                  }
                });

              case 10:
                _context.next = 12;
                return put({
                  type: 'flattenElements'
                });

              case 12:
                if (!content.css) {
                  _context.next = 21;
                  break;
                }

                _context.next = 15;
                return select(function (state) {
                  var _state$appBuilder$con;

                  return (_state$appBuilder$con = state.appBuilder.config) === null || _state$appBuilder$con === void 0 ? void 0 : _state$appBuilder$con.currentPage;
                });

              case 15:
                currentPage = _context.sent;
                _cssToSetting = cssToSetting(content.css, currentPage), _cssToSetting$tempora = _cssToSetting.temporary, temporary = _cssToSetting$tempora === void 0 ? {} : _cssToSetting$tempora, _cssToSetting$custom = _cssToSetting.custom, custom = _cssToSetting$custom === void 0 ? {} : _cssToSetting$custom, _cssToSetting$page = _cssToSetting.page, page = _cssToSetting$page === void 0 ? {} : _cssToSetting$page;
                _context.next = 19;
                return put({
                  type: 'style/save',
                  payload: {
                    customStyle: custom,
                    temporaryStyle: temporary,
                    pageStyle: page
                  }
                });

              case 19:
                _context.next = 23;
                break;

              case 21:
                _context.next = 23;
                return put({
                  type: 'style/save',
                  payload: {
                    temporaryStyle: {},
                    pageStyle: {}
                  }
                });

              case 23:
                if (!(content !== null && content !== void 0 && content.theme)) {
                  _context.next = 28;
                  break;
                }

                _content$theme = content === null || content === void 0 ? void 0 : content.theme, _name = _content$theme.name, url = _content$theme.url;
                loadTheme(_name, url);
                _context.next = 28;
                return put({
                  type: 'style/saveCurrentTheme',
                  payload: _objectSpread({}, content === null || content === void 0 ? void 0 : content.theme)
                });

              case 28:
              case "end":
                return _context.stop();
            }
          }
        }, setInitialPageJson);
      })
    },
    reducers: {
      flattenElements: function flattenElements(state) {
        var elements = getFlattenElements(state);
        return _objectSpread(_objectSpread({}, state), {}, {
          elements: elements
        });
      },
      updateAndFlattenElement: function updateAndFlattenElement(state, _ref3) {
        var payload = _ref3.payload;
        var element = payload.element;
        var newState = getNewState(state, element);
        var elements = getFlattenElements(newState);
        var page = newState.page;
        var updates = {
          elements: elements,
          page: page
        };

        if (element.type === 'form') {
          var formInstance = element.data.formInstance;

          if (formInstance) {
            updates.form = formInstance;
          }
        }

        return _objectSpread(_objectSpread(_objectSpread({}, state), newState), updates);
      },
      savePageData: function savePageData(state, _ref4) {
        var payload = _ref4.payload;
        var content = payload.content;
        var page = {};
        page.content = content;
        return _objectSpread(_objectSpread({}, state), {}, {
          page: page
        });
      },
      savePagePreview: function savePagePreview(state, _ref5) {
        var payload = _ref5.payload;
        return _objectSpread(_objectSpread({}, state), {}, {
          previewUrl: payload.previewUrl
        });
      },
      save: function save(state, action) {
        return _objectSpread(_objectSpread({}, state), action.payload);
      },
      updateEvents: function updateEvents(state, _ref6) {
        var payload = _ref6.payload;
        var events = payload.events,
            eventJs = payload.eventJs;
        var params = payload;

        if (!eventJs) {
          try {
            params.eventJs = compileModule(transpilePlugin(events)).default;
          } catch (error) {
            message.error('事件解析有误，请检查');
            console.error('事件解析有误，请检查', error);
          }
        }

        return _objectSpread(_objectSpread({}, state), params);
      },
      updateMacro: function updateMacro(state, _ref7) {
        var _ref7$payload = _ref7.payload,
            macros = _ref7$payload === void 0 ? [] : _ref7$payload;
        var macro = state.macro;
        macros.forEach(function (v) {
          if (!macro.some(function (item) {
            return item.value === v.value;
          })) {
            macro.push(v);
          }
        });
        return _objectSpread(_objectSpread({}, state), {}, {
          macro: macro
        });
      },
      reset: function reset(state) {
        var newState = {
          elements: {},
          // events: '',
          page: {},
          initialData: {},
          form: null
        };
        return _objectSpread(_objectSpread({}, state), newState);
      }
    }
  };
});
//# sourceMappingURL=appRender.js.map