/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { cloneDeep } from 'lodash';
import { h } from '@vue/composition-api';

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 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 => {
      v[levelName] = level;
      if (v.meta) {
        v.title = v.meta?.title || "";
        v.icon = v.meta?.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?.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);
      }
    }
  }
};
const renderIcon = (icon, url) => {
  const isString = typeof icon === "string";
  if (typeof icon === "function") {
    return h(icon, {
      slot: "icon"
    });
  }
  if (isString && icon.includes(".png")) {
    return h("img", {
      slot: "icon",
      class: "l-icon",
      attrs: {
        src: icon
      }
    });
  }
  if (isString && icon.includes(".svg")) {
    document.createElement("span");
    fetch(icon).then(res => res.text()).then(text => {});
  }
  if (isString && icon.includes("icon-")) {
    return h("l-icon", {
      slot: "icon",
      props: {
        url,
        name: icon
      }
    });
  }
  return null;
};
function hasChildren(item) {
  return item?.children?.filter(c => c.path).length > 0;
}

export { flattenRoutes, generateMenus, getActivePage, getExpandedPaths, getRoutesList, hasChildren, renderIcon };
//# sourceMappingURL=utils.js.map
