#!/bin/zsh

line=$(
  rbw list --raw \
    | jq -r '.[] | [.id, .name, .user // "-", ((.uris // []) | map(sub("^https?://";"") | sub("/.*$";"")) | join(","))] | @tsv' \
    | column -t -s$'\t' -o$'\t' \
    | fzf --delimiter=$'\t' --with-nth=2.. --tabstop=1 \
          --prompt='rbw > ' --header='NAME  USER  URIS'
) || exit 0                      # Esc / no match → clean exit

uuid=${line%%$'\t'*}             # field 1 = UUID
uuid=${uuid%% *}                 # trim column's padding spaces (UUIDs contain none)

output=("${(@f)$(rbw get --full "$uuid")}")

print -rn -- "$output[1]" | wl-copy                          # password → clipboard
print -rn -- "${output[2]#* }" | wl-copy -p                  # username → primary

if [[ $1 == "--show" ]]; then
  print -r -- "$output[1]"
fi
print -rl -- "${(@)output[2,-1]}"
