import { Component, Input } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Route, Router, RouterLink, RouterLinkActive } from '@angular/router';
import { filter } from 'rxjs';
import { CommonNzModule } from '../../../../../common.nz.module';

const mockPaths = [
  {
    path: '/manager/auth',
    icon: 'share-alt',
    label: '权限管理',
  },
  // {
  //   path: '/manager/task',
  //   icon: 'icons:task',
  //   label: '任务列表',
  // },
  // {
  //   path: '/manager/base-data',
  //   icon: 'icons:db',
  //   label: '基础数据',
  // },
  // {
  //   path: 'user',
  //   icon: 'icons:user',
  //   label: '用户管理',
  // },
  // {
  //   path: 'question',
  //   icon: 'icons:star',
  //   label: '题库管理',
  // },
  // {
  //   path: 'report',
  //   icon: 'icons:folder',
  //   label: '报表管理',
  // },
  // {
  //   path: 'dashboard',
  //   icon: 'icons:dashboard',
  //   label: '数据看板',
  // },
  // {
  //   path: 'ability',
  //   icon: 'icons:ability',
  //   label: '能力评估',
  // },
];

@Component({
  selector: 'sidebar-navigation',
  standalone: true,
  imports: [RouterLink, RouterLinkActive, CommonNzModule],
  templateUrl: './navigation.component.html',
  styleUrl: './navigation.component.less',
})
export class NavigationComponent {
  @Input() collapsed = false;
  paths: Route[] = [];

  constructor(
    private route: ActivatedRoute,
    private router: Router
  ) {
    router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => {
      const secondaryRoute = this.findSecondaryRoute();
      if (secondaryRoute) {
        this.paths =
          secondaryRoute.children?.map(child => ({
            ...child,
            path: `/manager/${secondaryRoute?.path}/${child.path}`,
          })) || [];
      }
    });
  }
  findSecondaryRoute(): Route | null {
    const currentUrl = this.router.url;
    const currentRoute = this.route.snapshot;
    const children = currentRoute.children;
    const secondaryRoutes = children.map(child => child.routeConfig);
    return secondaryRoutes.find(child => currentUrl.includes(child?.path || '')) || null;
  }
}
