/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { cloneDeep } from 'lodash';

const flattenRoutes = members => {
  if (!members) return;
  let children = [];
  const flattenMembers = members.map(m => {
    if (m.children && m.children.length) {
      children = [...children, ...m.children];
      delete m.children;
    }
    return m;
  });
  return flattenMembers.concat(children.length ? flattenRoutes(children) : children);
};
const getRoutesList = menus => {
  const copyMenus = cloneDeep(menus);
  return copyMenus.map(item => {
    item.children = flattenRoutes(item.children);
    return item;
  });
};
const generateMenus1 = (arr, path) => {
  if (!Array.isArray(arr)) return [];
  const p = path || "";
  arr.forEach(item => {
    var _item$meta, _item$meta2;
    item.title = ((_item$meta = item.meta) === null || _item$meta === void 0 ? void 0 : _item$meta.title) || "";
    item.icon = ((_item$meta2 = item.meta) === null || _item$meta2 === void 0 ? void 0 : _item$meta2.icon) || "";
    if (p && item.path) item.path = `${p}/${item.path}`;
    if (item.children) generateMenus1(item.children, item.path);
  });
  return arr;
};
const generateMenus = function (array) {
  let levelName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "level";
  let childrenName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "children";
  if (!Array.isArray(array)) return [];
  const recursive = function (array2) {
    let path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
    let level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
    level++;
    return array2.map(v => {
      var _v$meta, _v$meta2;
      v[levelName] = level;
      v.title = ((_v$meta = v.meta) === null || _v$meta === void 0 ? void 0 : _v$meta.title) || "";
      v.icon = ((_v$meta2 = v.meta) === null || _v$meta2 === void 0 ? void 0 : _v$meta2.icon) || "";
      const child = v[childrenName];
      if (path && v.path) v.path = `${path}/${v.path}`;
      if (child && child.length) recursive(child, v.path, level);
      return v;
    });
  };
  return recursive(array);
};
const getActivePage = route => {
  return route === null || route === void 0 ? void 0 : route.path;
};
const getExpandedPaths = (data, path) => {
  if (data.length === 0) return;
  for (let i = 0; i < data.length; i++) {
    if (data[i].path === path) {
      return [];
    } else {
      if (data[i].children) {
        const res = getExpandedPaths(data[i].children, path);
        if (res !== void 0) return res.concat(data[i].path);
      }
    }
  }
};

export { flattenRoutes, generateMenus, generateMenus1, getActivePage, getExpandedPaths, getRoutesList };
//# sourceMappingURL=menu.js.map
