#!/bin/sh
set -eu

#sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)"
sessions="$(tmux list-windows -a -F '#S #W::#S:#I' 2>/dev/null || true)"

[ -n "${sessions}" ] || exit 0

walker_pick_session() {
  if command -v walker >/dev/null 2>&1; then
    walker -d -p 'tmux sessions:'
  fi
}

rofi_pick_session() {
    if command -v rofi >/dev/null 2>&1; then
        rofi -i -dmenu -display-columns 1 -display-column-separator '::' -p 'tmux sessions:'
    fi
}

chosen="$(printf '%s\n' "$sessions" | rofi_pick_session | awk -F'::' 'NR==1{print $NF}')"

[ -n "$chosen" ] || exit 0

if [ "${TMUX-}" ]; then
  exec tmux switch-client -t "${chosen}"
fi

session_name="${chosen%%:*}"
tmux select-window -t $chosen && hyprctl dispatch "focus_or_launch({match = \"$session_name\", field = \"title\", cmd = \"foot -e tmux attach-session -t $chosen\"})"
