import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";

/*
 * @Descripttion : 存储页面编辑的一些动作信息
 */
import dotProp from 'dot-prop-immutable'; // TODOS

import { getPlugin } from "../plugins";
import { formBuilderConfig } from "../config/config";
var _formBuilderConfig$pr = formBuilderConfig.previewDevice,
    previewDevice = _formBuilderConfig$pr === void 0 ? '' : _formBuilderConfig$pr;
export default {
  namespace: 'ui',
  state: {
    activeElement: null,
    dragging: false,
    highlightElement: null,
    plugins: {},
    resizing: false,
    activeDrawer: null,
    dragElement: {},
    dragComponent: '',
    // 预览设备
    previewDevice: previewDevice
  },
  reducers: {
    /**
     * @description: 激活插件，元素和block弹出框
     * @param { name:插件name }
     * @return:
     */
    togglePlugin: function togglePlugin(state, _ref) {
      var payload = _ref.payload;
      var name = payload.name,
          params = payload.params,
          _payload$closeOtherIn = payload.closeOtherInGroup,
          closeOtherInGroup = _payload$closeOtherIn === void 0 ? false : _payload$closeOtherIn;
      var plugin = getPlugin(name);

      if (!plugin) {
        return _objectSpread({}, state);
      }

      var typePlugins = dotProp.get(state, "plugins.".concat(plugin.type));

      if (!Array.isArray(typePlugins)) {
        typePlugins = [];
      }

      var alreadyActive = typePlugins.findIndex(function (pl) {
        return pl.name === plugin.name;
      });

      if (alreadyActive > -1) {// typePlugins = dotProp.delete(typePlugins, alreadyActive);
      } else if (closeOtherInGroup) {
        typePlugins = [{
          name: name,
          params: params
        }];
      } else {
        typePlugins.push({
          name: name,
          params: params
        });
      }

      return dotProp.set(state, "plugins.".concat(plugin.type), typePlugins);
    },

    /**
     * @description: 切换插件
     * @param { name:插件name }
     * @return:
     */
    changePlugin: function changePlugin(state, _ref2) {
      var payload = _ref2.payload;
      var name = payload.name,
          params = payload.params,
          _payload$closeOtherIn2 = payload.closeOtherInGroup,
          closeOtherInGroup = _payload$closeOtherIn2 === void 0 ? false : _payload$closeOtherIn2;
      var plugin = getPlugin(name);

      if (!plugin) {
        return _objectSpread({}, state);
      }

      var typePlugins = dotProp.get(state, "plugins.".concat(plugin.type));

      if (!Array.isArray(typePlugins)) {
        typePlugins = [];
      }

      var alreadyActive = typePlugins.findIndex(function (pl) {
        return pl.name === plugin.name;
      });
      typePlugins = [{
        name: name,
        params: params
      }];
      return dotProp.set(state, "plugins.".concat(plugin.type), typePlugins);
    },

    /**
     * @description: 去激活插件，元素和block弹出框
     * @param { name:插件name }
     * @return:
     */
    deactivatePlugin: function deactivatePlugin(state, _ref3) {
      var payload = _ref3.payload;
      var name = payload.name;
      var plugin = getPlugin(name);

      if (!plugin) {
        return state;
      }

      var typePlugins = dotProp.get(state, "plugins.".concat(plugin.type));

      if (!Array.isArray(typePlugins)) {
        typePlugins = [];
      }

      var alreadyActive = typePlugins.findIndex(function (pl) {
        return pl.name === plugin.name;
      });

      if (alreadyActive > -1) {
        typePlugins = dotProp.delete(typePlugins, alreadyActive);
      }

      return dotProp.set(state, "plugins.".concat(plugin.type), typePlugins);
    },
    dragStart: function dragStart(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        // dragging: true,
        highlightElement: null
      });
    },
    dragEnd: function dragEnd(state) {
      return _objectSpread({}, state);
    },
    resizeStart: function resizeStart(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        resizing: true
      });
    },
    resizeStop: function resizeStop(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        resizing: false
      });
    },
    // 高亮元素，鼠标hover
    highlightElement: function highlightElement(state, _ref4) {
      var payload = _ref4.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        highlightElement: payload.element ? payload.element : null
      });
    },
    // 激活元素，鼠标点击展示setting
    activatePageElement: function activatePageElement(state, _ref5) {
      var payload = _ref5.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        activeElement: payload.element
      });
    },
    deactivateElement: function deactivateElement(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        activeElement: null
      });
    },
    updateUi: function updateUi(state, _ref6) {
      var payload = _ref6.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    dragElement: function dragElement(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    dragComponent: function dragComponent(state, _ref8) {
      var payload = _ref8.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    // 更新远程系统的数据
    updateRemoteData: function updateRemoteData(state, _ref9) {
      var payload = _ref9.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    changePreviewDevice: function changePreviewDevice(state, _ref10) {
      var payload = _ref10.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        previewDevice: payload.previewDevice ? payload.previewDevice : previewDevice
      });
    },
    // 清空 toolbar preview 设置
    clearToolbarPreview: function clearToolbarPreview(state) {
      return dotProp.set(state, 'plugins', {});
    }
  },
  effects: {
    activateElement: /*#__PURE__*/_regeneratorRuntime.mark(function activateElement(_ref11, _ref12) {
      var payload, put;
      return _regeneratorRuntime.wrap(function activateElement$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref11.payload;
              put = _ref12.put;
              _context.next = 4;
              return put({
                type: 'globalUI/save',
                payload: {
                  propertyMode: 'page'
                }
              });

            case 4:
              _context.next = 6;
              return put({
                type: 'activatePageElement',
                payload: payload
              });

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, activateElement);
    })
  }
};
//# sourceMappingURL=ui.js.map