import { type ReposConfiguration } from "../../src/configTypes.ts"

export const NavHelper = (reposConfig: ReposConfiguration, slugify: Function, repoName: string, branchName: string) => {
  const reposPath = reposConfig.path || ""

  // These two aren't actually used by any pages, but they're in almost
  // every page URL. E.g. all of them start with 'repos/my-repo-name'
  // or 'repos/my-repo-name/branches.'
  const repoBasePath = `${reposPath}/${slugify(repoName)}`
  const rootBasePathBranches = `${repoBasePath}/branches`

  const currentBranchPath = `${rootBasePathBranches}/${slugify(branchName)}`
  const repoBranchCommitsBase = `${currentBranchPath}/commits/`

  return {
    rootPath: () => {
      if (reposPath === "") {
        return "/"
      }
      else {
        return reposPath
      }
    },
    repoHomePath: () => {
      return `${rootBasePathBranches}/${slugify(reposConfig.repos[repoName].defaultBranch)}`
    },
    repoCurrentBranchHome: () => {
      return currentBranchPath
    },
    repoBranchHome: (branchName: string) => {
      return `${rootBasePathBranches}/${slugify(branchName)}`
    },
    repoCurrentBranchFiles: () => {
      return `${currentBranchPath}/files`
    },
    repoBranchCommitsBase: () => repoBranchCommitsBase,
    repoCurrentBranchCommits: () => `${repoBranchCommitsBase}page1`,
    repoCurrentBranchBranches: () => `${currentBranchPath}/branches`,
    repoCurrentBranchRssFeed: () => `${currentBranchPath}/commits.xml`,
    homepageButtons: reposConfig.repos[repoName].defaultTemplateConfiguration?.homepageButtons || []
  }
}

