////
// tab completion

import { cmdInput } from "./dom"
import { commands } from "./commands"

export function initCompletion() {
  cmdInput.addEventListener("keydown", handleCompletion)
}

function handleCompletion(e: KeyboardEvent) {
  if (e.key !== "Tab") return

  e.preventDefault()
  const input = cmdInput.value

  for (const command of Object.keys(commands)) {
    if (command.startsWith(input)) {
      cmdInput.value = command + " "
      return
    }
  }
}