import { runCommand } from "./shell.js"
import { focusInput } from "./focus.js"

export function initHyperlink() {
  window.addEventListener("click", handleClick)
}

function handleClick(e: MouseEvent) {
  const target = e.target

  if (!(target instanceof HTMLElement)) return

  const a = target.closest("a")
  if (!a) return

  const href = a.getAttribute("href")
  if (!href) return

  if (href.startsWith("#")) {
    e.preventDefault()
    runCommand(href.slice(1))
    focusInput()
  }
}