/*
 * @Author: 赵伟
 * @Date: 2024-09-02 08:42:57
 * @Description: 自定义面包屑，暂时不用，使用了 ProBreadcrumb
 */

import { Breadcrumb, type BreadcrumbProps } from 'antd';
import { Link, matchPath, useLocation } from 'umi';
// import routes from '../../../config/config'; // 导入你的路由配置
type Route = {
  path: string;
  breadcrumb?: string;
  routes?: Route[];
  redirect?: string;
  name?: string;
  component?: string;
  layout?: boolean;
  key?: string;
};

const routes: Route[] = [];

const KFBreadcrumb = () => {
  const location = useLocation();

  const items: BreadcrumbProps['items'] = [];

  // 遍历路由表，生成面包屑数据
  const generateBreadcrumbs = (pathname: string, routes: Route[], prefix: string = '') => {
    for (const route of routes) {
      if (route.redirect || route.layout === false || !route.path || route.path === '*') {
        continue;
      }
      const match = matchPath(
        { path: `${prefix}/${route.path}`, end: route.routes ? false : true },
        pathname,
      );
      if (match) {
        items.push({
          path: route.path.startsWith('/') ? route.path : `${prefix}/${route.path}`,
          title: <Link to={route.path}>{route.breadcrumb}</Link>,
        });
      }
      if (route.routes) {
        generateBreadcrumbs(pathname, route.routes, `${prefix}/${route.path}`);
      }
    }
  };

  generateBreadcrumbs(location.pathname, routes);

  // const itemRender = (route, params, routes, paths) => {};

  return <Breadcrumb items={items}></Breadcrumb>;
};

export default KFBreadcrumb;
