package solutions.tretter.githugandroid

internal fun fileCompletionCandidates(repo: RepoState): List<String> {
    val prefix = repo.currentDirPrefix()
    return repo.files
        .filterNot { it.deleted }
        .map { it.name }
        .filter { it.startsWith(prefix) }
        .map { it.removePrefix(prefix) }
        .filter { it.isNotBlank() }
}

internal fun directoryCompletionCandidates(repo: RepoState): List<String> {
    val prefix = repo.currentDirPrefix()
    return repo.files
        .filterNot { it.deleted }
        .map { it.name }
        .filter { it.startsWith(prefix) }
        .map { it.removePrefix(prefix) }
        .flatMap { file ->
            val parts = file.split('/').dropLast(1)
            parts.indices.map { index -> parts.take(index + 1).joinToString("/") + "/" }
        }
        .distinct()
}

private fun RepoState.currentDirPrefix(): String {
    return if (currentDir == ".") "" else currentDir.trimEnd('/') + "/"
}
