import "core-js/modules/es.array.reduce.js";
export const getOpenKeysFromMenuData = menuData => {
  return (menuData || []).reduce((pre, item) => {
    if (item.key) {
      pre.push(item.key);
    }
    if (item.children) {
      const newArray = pre.concat(getOpenKeysFromMenuData(item.children) || []);
      return newArray;
    }
    return pre;
  }, []);
};
const themeConfig = {
  daybreak: 'daybreak',
  '#1890ff': 'daybreak',
  '#F5222D': 'dust',
  '#FA541C': 'volcano',
  '#FAAD14': 'sunset',
  '#13C2C2': 'cyan',
  '#52C41A': 'green',
  '#2F54EB': 'geekblue',
  '#722ED1': 'purple'
};
const invertKeyValues = obj => Object.keys(obj).reduce((acc, key) => {
  acc[obj[key]] = key;
  return acc;
}, {});

/**
 * #1890ff -> daybreak
 *
 * @param val
 */
export function genThemeToString(val) {
  return val && themeConfig[val] ? themeConfig[val] : undefined;
}

/**
 * Daybreak-> #1890ff
 *
 * @param val
 */
export function genStringToTheme(val) {
  const stringConfig = invertKeyValues(themeConfig);
  return val && stringConfig[val] ? stringConfig[val] : val;
}
export function clearMenuItem(menusData) {
  return menusData.map(item => {
    const finalItem = {
      ...item
    };
    if (!finalItem.name || finalItem.hideInMenu) {
      return null;
    }
    if (finalItem && finalItem !== null && finalItem !== void 0 && finalItem.children) {
      if (!finalItem.hideChildrenInMenu && finalItem.children.some(child => child && child.name && !child.hideInMenu)) {
        return {
          ...item,
          children: clearMenuItem(finalItem.children)
        };
      }
      // children 为空就直接删掉
      delete finalItem.children;
    }
    return finalItem;
  }).filter(item => item);
}