import { RouteModel } from "../models"

interface ComponentRoutesModel {
  targets: string [],
  component: string
}

const findRoutes = (routes: RouteModel[] = [], component: string | undefined,  parentTarget: string [] = [], result: ComponentRoutesModel [] = []): ComponentRoutesModel [] => {

  if(!Array.isArray(routes))
    return []

  routes.forEach((route: RouteModel) => {
    const targets: string [] = []
    if(parentTarget) {
      parentTarget.forEach(item => {
        targets.push(item)
      })
    }
    targets.push(route.target)

    if(route.component) {
      result.push({
        targets,
        component: route.component
      })
    }

    if(Array.isArray(route.childRoutes)) {
      findRoutes(route.childRoutes, component, targets, result)
    }
  })

  if(component) {
    result = result.filter(x => x.component === component)
  }

  return result
}

export {
  findRoutes
}