#!/usr/bin/env bash
set -euo pipefail

menu_name="${1:-}"
if [[ -z "$menu_name" ]]; then
  tmux display-message "which-key menu name required"
  exit 1
fi

menu_option="@wk_menu_${menu_name}"
menu_payload="$(tmux show -gv "$menu_option" 2>/dev/null || true)"
if [[ -z "$menu_payload" ]]; then
  tmux display-message "unknown which-key menu: $menu_name"
  exit 1
fi

pos_x="$(tmux show -gv @wk_cfg_pos_x 2>/dev/null || echo R)"
pos_y="$(tmux show -gv @wk_cfg_pos_y 2>/dev/null || echo P)"
title_prefix="$(tmux show -gv @wk_cfg_title_prefix 2>/dev/null || echo tmux)"
title_style="$(tmux show -gv @wk_cfg_title_style 2>/dev/null || echo 'align=centre,bold')"
prefix_style="$(tmux show -gv @wk_cfg_title_prefix_style 2>/dev/null || echo 'fg=green,align=centre,bold')"
title="#[${title_style}]#[${prefix_style}]${title_prefix} #[${title_style}]${menu_name}"

# menu_payload is generated by tmux-which-key and intentionally shell-quoted.
eval "set -- ${menu_payload}"
exec tmux display-menu -x "$pos_x" -y "$pos_y" -T "$title" "$@"
