
function isOutlink(path) {
  return path?.startsWith('http://') || path?.startsWith('https://');
}

export function flatten(routes = [], parent = '') {
  const res = {};
  for (let item of routes) {
    const path = isOutlink(item.path)
      ? item.path
      : [parent, item.path].join('/').replace(/\/\//g, '/');

    const node = { [path]: item }
    Object.assign(res, node);

    if (Array.isArray(item.children)) {
      const children = flatten(item.children, path)
      Object.assign(res, children);
    }
  }

  return res;
}
