const legacySubSystemPrefixes = ['/pipeNetwork', '/waterSupply']

export function isSubSystemPath(path = '') {
  return path.startsWith('/subSystem/') || legacySubSystemPrefixes.some(prefix => path === prefix || path.startsWith(`${prefix}/`))
}

export function getCurrentSystemPrefix(path = '') {
  if (path.startsWith('/subSystem/')) {
    return path.split('/').slice(0, 3).join('/')
  }
  return legacySubSystemPrefixes.find(prefix => path === prefix || path.startsWith(`${prefix}/`)) || ''
}

export function selectSidebarRoutes(currentPath, allRoutes = []) {
  if (currentPath.startsWith('/portal')) {
    return []
  }

  if (isSubSystemPath(currentPath)) {
    const currentSystemPrefix = getCurrentSystemPrefix(currentPath)
    return allRoutes.filter(item => currentSystemPrefix && item.path.startsWith(currentSystemPrefix))
  }

  return allRoutes.filter(item => !isSubSystemPath(item.path))
}

export function getSubsystemHomePath(prefix) {
  if (prefix === 'waterSupply') {
    return '/subSystem/waterSupply/index'
  }
  return `/${prefix}/index`
}
