package solutions.tretter.githugandroid

object GitSandboxEngine {
    data class ShellToken(
        val value: String,
        val quoted: Boolean = false,
    )

    fun parsePatchHunkEditorInvocation(repo: RepoState, command: String): GitEditorInvocation? =
        InteractiveAddEngine.parsePatchHunkEditorInvocation(repo, command)

    fun applyPatchHunkEdit(repo: RepoState, content: String): Pair<RepoState, List<String>> =
        InteractiveAddEngine.applyPatchHunkEdit(repo, content)

    fun commandReferenceLines(): List<String> = listOf(
        "Available sandbox commands:",
        "  git ",
        "  ls|dir",
        "  touch <file>",
        "  help",
        "  pwd ",
        "  cat <file>",
        "  touch <file>",
        "  mkdir|md <directory>",
        "  cd <directory>",
        "  cd..",
        "  rm|del <file>",
        "  echo <message>",
    )

    fun execute(repo: RepoState, command: String): Pair<RepoState, List<String>> {
        val shellParts = tokenizeShellCommand(command)
        if (shellParts.isEmpty()) return repo to emptyList()
        repo.interactiveAddSession?.let {
            return InteractiveAddEngine.handleInput(repo, command)
        }
        return SandboxCommandEngine.execute(repo, shellParts)
    }

    fun tokenizeCommand(command: String): List<String> {
        return SandboxShell.tokenizeCommand(command)
    }

    fun tokenizeShellCommand(command: String): List<ShellToken> {
        return SandboxShell.tokenizeShellCommand(command)
    }

    fun expandPathspecs(repo: RepoState, arguments: List<String>): List<String> {
        return SandboxShell.expandPathspecs(repo, arguments)
    }

    fun expandPathspecTokens(repo: RepoState, arguments: List<ShellToken>): List<String> {
        return SandboxShell.expandPathspecTokens(repo, arguments)
    }
}
