import { useEffect, useRef } from 'react';
// 设计模式下状态管理
var useDesignMode = function useDesignMode(props, _ref) {
  var registerValueVariable = _ref.registerValueVariable,
    openDrawer = _ref.openDrawer,
    closeDrawer = _ref.closeDrawer,
    data = _ref.data;
  var __designMode = props.__designMode,
    componentId = props.componentId,
    operation = props.operation;
  var _ref2 = operation || {},
    items = _ref2.items;

  // 存储上一次的items
  var prevItemsRef = useRef([]);
  useEffect(function () {
    var _prevItemsRef$current;
    if (__designMode === 'design' && items !== null && items !== void 0 && items.length && (_prevItemsRef$current = prevItemsRef.current) !== null && _prevItemsRef$current !== void 0 && _prevItemsRef$current.length) {
      // 找出变更的那一条记录
      var changedItem = items.find(function (item) {
        var prevItem = prevItemsRef.current.find(function (prev) {
          return prev.key === item.key;
        });
        if (!prevItem) return false;

        // 对比actionType是否变更
        if (item.actionType !== prevItem.actionType) {
          return true;
        }
        return false;
      });
      if (changedItem) {
        // 如果是view/edit/custom类型,则打开对应弹窗
        if (['view', 'edit', 'custom'].includes(changedItem.actionType)) {
          registerValueVariable === null || registerValueVariable === void 0 ? void 0 : registerValueVariable(componentId, {
            currentRecord: (data === null || data === void 0 ? void 0 : data[0]) || null
          });
          openDrawer(changedItem);
        } else {
          closeDrawer();
        }
      }
    }

    // 更新prevItemsRef
    prevItemsRef.current = items;
  }, [items, __designMode]);
};
export default useDesignMode;