#compdef wl-paste

local expl
typeset -A opt_args

_wl-paste_types() {
	local -a types args
	args=(
		${(kv)opt_args[(I)-p|--primary]}
		${(kv)opt_args[(I)-s|--seat]}
	)
	types=( ${(@f)"$(wl-paste $args -l 2>/dev/null)"} )

	local expl
	_wanted types expl 'type' compadd "$@" -a types
}

__all_seats() {
	local -a seats
	if (( $+commands[weston-info] && $+commands[sed] )); then
		seats=( ${(@f)"$(weston-info 2>/dev/null | sed -n '/wl_seat/{n;s/\s*name: //;p}')"} )
	elif (( $+commands[loginctl] )); then
		seats=( ${(@f)"$(loginctl --no-legend --no-pager list-seats 2>/dev/null)"} )
	fi

	if [[ -z $seats ]]; then
		# seat0 covers most cases, so its a good fallback.
		compadd "$@" - seat0
	else
		compadd "$@" -a seats
	fi
}

_arguments -S -s \
	{-n,--no-newline}'[Do not append a newline character]' \
	{-l,--list-types}'[Instead of pasting, list the offered types]' \
	{-p,--primary}'[Use the "primary" clipboard]' \
	{-w,--watch}'[Run a command wach time the selection changes]:*::command:_normal' \
	{-t+,--type=}'[Override the inferred MIME type for the content]:mimetype:_wl-paste_types' \
	{-s+,--seat=}'[Pick the seat to work with]:seat:__all_seats' \
	{-v,--version}'[Display version info]' \
	{-h,--help}'[Display a help message]'
