#!/bin/bash
# Fuzzy find email addresses with fzf

QUERY="$1"

# Get all addresses from notmuch
ADDRESSES=$(notmuch address --output=sender --output=recipients \
  --deduplicate=address '*' | sort -u)

# If query is provided, pre-filter
if [ -n "$QUERY" ]; then
  ADDRESSES=$(echo "$ADDRESSES" | grep -i "$QUERY")
fi

# Use fzf for selection
SELECTED=$(echo "$ADDRESSES" | fzf \
  --height=40% \
  --layout=reverse \
  --border \
  --prompt="Select recipient: " \
  --preview="" \
  --query="$QUERY")

# Output selected address
echo "$SELECTED"
