#!/bin/bash
set -euo pipefail
client=$1
pane=$2
position=${3:-mouse}
[[ "$pane" =~ ^%[0-9]+$ ]] || exit 1
[[ "$client" =~ ^/dev/pts/[0-9]+$ ]] || exit 1
if [ "$position" = mouse ]; then x=M; y=M; else x=C; y=C; fi
select_all="copy-mode -t $pane ; send-keys -X -t $pane history-top ; send-keys -X -t $pane start-of-line ; send-keys -X -t $pane begin-selection ; send-keys -X -t $pane history-bottom ; send-keys -X -t $pane end-of-line"
copy_selection="send-keys -X -t $pane copy-pipe-and-cancel \"tmux load-buffer -w -t '$client' -\""
tmux display-menu -O -c "$client" -t "$pane" -T 'Historial y manual — ia-lab' -x "$x" -y "$y" \
  'Ir hasta arriba       Ctrl+A g' g "copy-mode -t $pane ; send-keys -X -t $pane history-top" \
  'Ir hasta abajo        Ctrl+A G' G "copy-mode -t $pane ; send-keys -X -t $pane history-bottom ; send-keys -X -t $pane cancel" \
  'Seleccionar todo      Ctrl+A a' a "$select_all" \
  'Copiar seleccion      Enter / y' y "$copy_selection" \
  'Copiar todo ahora' c "$select_all ; $copy_selection" \
  '' \
  'Manual de nuestros comandos' m "display-popup -c '$client' -t $pane -E -w 90% -h 85% -T 'Manual ia-lab | q: cerrar' 'less -R /home/lab/.config/tmux/manual.txt'" \
  '' \
  'Cerrar menu' q ''
