zeditor-here() (
  for fqdn in $(printf "%q\n" $(hostname -f) $(hostname -A) | sort -u); do
    printf "zeditor ssh://%q:%q\n" "${fqdn}" "$(realpath "${1:-.}")"
  done
)

# zed-here()  { zeditor-here "$@"; }
atom-here() { zeditor-here "$@"; }


if [[ -z "${SSH_CLIENT:-}" && -z "${SSH_TTY:-}" ]];
then


zeditor-remote() {
  local statedir="${XDG_STATE_HOME:-"$HOME/.local/state/"}/zeditor-remote-sh"

  local -a missing=()
  command -v xe >/dev/null || missing+=('xe')
  command -v fd >/dev/null || missing+=('fd')
  command -v rg >/dev/null || missing+=('rg')
  command -v fzf >/dev/null || missing+=('fzf')
  command -v gum >/dev/null || missing+=('gum')
  if [[ "${#missing[@]}" -gt 0 ]]; then
    printf >&2 "%s\n" "ERROR: Not found in PATH:$(printf " %s" "${missing[@]}")"
    return 1
  fi
  if [[ ! -v HOME ]]; then
    printf >&2 "%s\n" "ERROR: $HOME is not set"
    return 1
  fi

  uniq-stable() (
    command cat -n | sort -b --key=2.1 -u | sort -n | cut -f2-
  )

  local hosts=$(
    {
      printf '%s\n' localhost
      {
        if [[ -s "$statedir"/ssh-host-history ]]; then
          cat "$statedir"/ssh-host-history
        fi

        # ask tailscale
        if command -v tailscale >/dev/null; then
          # tailscale status --json | jq .Peer[].HostName -r
          # tailscale status --json | jq '.Peer[].DNSName | rtrimstr(".")' -r
          tailscale status --json | jq '.MagicDNSSuffix as $suffix | .Peer[].DNSName | rtrimstr(".") | rtrimstr("." + $suffix)' -r
        fi

        # search .ssh/known_hosts, strip DNS search domains to deduplicate
        if [[ -r /etc/resolv.conf && -r "$HOME"/.ssh/known_hosts ]]; then
          local -a hosts
          readarray -d $'\n' -t hosts < <(
            cut <"$HOME"/.ssh/known_hosts -d' ' -f1 | sort -u \
              | grep -vE '^\[' \
              | grep -v '[,@]' \
              | grep -Ev '^([0-9]{0,3}\.){3}[0-9]{0,3}$' \
              | grep -Ev '^([0-9a-fA-F]{0,4}:){0,7}:?([0-9a-fA-F]{0,4}:){0,6}[0-9a-fA-F]{0,4}$'
          )
          local -a domains
          readarray -d ' ' -t domains < <(
            grep </etc/resolv.conf '^search ' | cut -d' ' -f2-
          )
          printf '%s\n' "${hosts[@]}" \
            | sed -E "s/\\.($(IFS='|'; printf "%s" "${domains[*]}"))\$//g" \
            | grep -v '[.:]'
        fi
      } | grep -v '^localhost$' | uniq-stable
    } | fzf --sync --multi --reverse --bind 'ctrl-a:toggle-all' --height=25 --cycle \
      | sed -e 's/%/%%/g'
  )
  if [[ -z "$hosts" ]]; then
    return 1
  fi

  local prefix="$(gum input --value="repos/")"
  # make $prefix relative to $HOME
  if [[ "$prefix" =~ ^/ ]]; then
    prefix="${prefix//"$HOME/"/}"
  fi
  if [[ "$prefix" =~ ^/ ]]; then
    printf >&2 "%s\n" "ERROR: prefix not in \$HOME ($prefix)"
    return 1
  fi
  # remove trialing slash
  while [[ "$prefix" =~ /$ ]]; do
    prefix="${prefix/%"/"/}"
  done
  if [[ -z "$prefix" ]]; then
    printf >&2 "%s\n" "ERROR: prefix chosen"
    return 1
  fi


  # local spin=(gum spin --show-output --show-error --)
  local spin=(gum spin --show-output --)
  local repos=$(
      export prefix statedir
      worker() {
        local host="$1"
        # TODO: this assumes remote hosts has 'fd', use 'find' instead?
        fdargs=(--hidden --no-ignore --max-depth 5)
        # accept all, persist nothing, to scan hosts we've yet to trust, zed will still complain however
        sshargs=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)
        if [[ "$host" = localhost ]]; then
          cd "$HOME"
          fd "^\.git$" "$prefix/" "${fdargs[@]}" -L -x printf '~/%q\n' "{//}"
        else
          # TODO: report timeouts?
          # TODO: auto escape this
          timeout 5 ssh "${sshargs[@]}" 2>/dev/null "$host" \
            fd "^\.git$" "'$prefix/'" "${fdargs[@]}" -L -x printf "'ssh://%s/~/%q\n'" "'$host'" "'{//}'"
        fi
      }
      "${spin[@]}" xe -f <(printf "%s\n" "$hosts") -j"$(wc -l <<<"$hosts")" -s "$(declare -f worker); worker \"\$@\"" ||:
  )
  if [[ -z "${repos}" ]]; then
    printf >&2 '%s\n' "ERROR: no repos found!"
    return 1
  fi

  local choice=$(
    {
      if [[ -s "$statedir"/ssh-repo-history ]]; then
        grep <"$statedir"/ssh-repo-history "$(printf "^ssh://%s/\n" "${hosts[@]}")"
      fi
      sort <<<"$repos"
    } | uniq-stable | fzf -sync --reverse --cycle --height=25)
  if [[ -z "${choice}" ]]; then
    return
  fi

  local zed_cmd="zeditor ${choice}"
  local host=$(
    rg <<<"$choice" '^ssh://([^/]*)/(~/.*)$' -r '$1' || printf "%s\n" localhost
  )
  local ssh_cd_cmd=$(
    rg <<<"$choice" '^ssh://([^/]*)/(~/.*)$' -r 'ssh -t "$1" "cd $2; \$$SHELL -l"' ||:
  )

  # TODO: find a way for this definition not to leak out of the function
  # TODO: evict
  lru_push() {
    local line="$1"
    local fname="$2"
    mkdir -p "$(dirname "$fname")"
    local new_lru=$(
      printf "%s\n" "$line"
      [[ -s "$fname" ]] && cat "$fname"
    )
    uniq-stable <<<"$new_lru" >"$fname"
  }

  if [[ -n "$ssh_cd_cmd" ]]; then
    if gum confirm "Open zed?" --default=yes; then
      # history -s "${zed_cmd}" # TODO: this too?
      eval ${zed_cmd}
    elif [[ "$?" -eq 130 ]]; then
      return 1
    fi

    lru_push "$host"   "$statedir"/ssh-host-history
    lru_push "$choice" "$statedir"/ssh-repo-history

    # TODO: find a way to not push the rest of the unpushed history
    history -s "${ssh_cd_cmd}"
    history -a
    if gum confirm "SSH in there now?" --default=yes; then
      printf >&2 "%s\n" "+ ${ssh_cd_cmd}"
      eval ${ssh_cd_cmd}
      do_ssh=true
    elif [[ "$?" -eq 130 ]]; then
      return 1
    fi
  else
    if command -v direnv >/dev/null; then
      # TODO: need i find a way to load the direnv on remotes?
      # https://github.com/zed-industries/zed/issues/4977#issuecomment-2536680602
      zed_cmd="direnv exec ${choice} $zed_cmd"
    fi

    if gum confirm "Open zed?" --default=yes; then
      # history -s "${zed_cmd}"
      eval ${zed_cmd}
    elif [[ "$?" -eq 130 ]]; then
      return 1
    fi

    # lru_push "$host"   "$statedir"/ssh-host-history
    lru_push "$choice" "$statedir"/ssh-repo-history

    printf >&2 "%s\n" "+$(printf " %q" cd "${choice}")"
    eval cd "${choice}" # 'eval' to expand the tilde
  fi
  unset -f lru_push
}

# zed-remote()  { zeditor-remote "$@"; }
atom-remote() { zeditor-remote "$@"; }


fi
