//// 
// Hack... works with the `upload` command.
// Dragging a file to the terminal fills in the <input type="file"/> on screen.

export function initDrop() {
  ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
    document.body.addEventListener(eventName, preventDefaults, false);
  })

  document.body.addEventListener("drop", handleDrop)
}

function preventDefaults(e: Event) {
  e.preventDefault()
  e.stopPropagation()
}

function handleDrop(e: DragEvent) {
  const fileInput = document.querySelector("input[type=file]") as HTMLInputElement

  const files = e.dataTransfer?.files ?? []

  if (files.length > 0) {
    const dt = new DataTransfer()
    Array.from(files).forEach(f => dt.items.add(f))
    fileInput.files = dt.files
    fileInput.dispatchEvent(new Event('change', { bubbles: true }))
  }
}