import { type Repository } from "./dataTypes.ts"

let cachedBranches: Array<{
  branchName: string,
  repoName: string,
}> | null = null

export default (repos: Array<Repository>) => {
  if (cachedBranches !== null) { return cachedBranches }

  cachedBranches = repos.flatMap((repo) => {
    return repo.branches.map((branch) => {
      const result = {
        branchName: branch.name,
        repoName: repo.name,
        compareTo: branch.compareTo,
        ahead: branch.ahead,
        behind: branch.behind,
      }
      if (branch.description) { result['description'] = branch.description }
      return result
    })
  })

  return cachedBranches
}
