/*
 * @Description: 动态面板引用面板的使用限制
 * @Author: taosu
 * @LastEditors: taosu
 */
import { isPanel, isGroup, isRefScreen } from 'utils/utils';

export const setPanelLimit = (type, user, list) => {
  const { userInfo } = user;
  const { panelLimit } = userInfo;
  let groupLayNum = 0;

  if (panelLimit) {
    switch (type) {
      case 'referencePanelCountLimit': {
        const linkList = list.filter((d) => isRefScreen(d.id));
        const groups = list.filter((d) => isGroup(d.id));
        if (groups.length > 0) {
          groups.map((item) => {
            item.components.map((d) => {
              if (isRefScreen(d.id)) {
                groupLayNum += 1;
              }
            });
          });
        }
        return linkList.length + groupLayNum > panelLimit.referencePanelCountLimit - 1;
      }
      case 'dynamicPanelCountLimit': {
        const panelList = list.filter((d) => isPanel(d.id));
        const groups = list.filter((d) => isGroup(d.id));
        if (groups.length > 0) {
          groups.map((item) => {
            item.components.map((d) => {
              if (isPanel(d.id)) {
                groupLayNum += 1;
              }
            });
          });
        }
        return panelList.length + groupLayNum > panelLimit.dynamicPanelCountLimit - 1;
      }
      case 'dynamicPanelStateLimit': {
        return list.length > panelLimit.dynamicPanelStateLimit - 1;
      }
      case 'referencePanelStateLimit': {
        return list.length > panelLimit.referencePanelStateLimit - 1;
      }
      default:
        return true;
    }
  } else {
    return false;
  }
};
