# restore the default prompt
rlx.prompt() {
  console prompt --program "";
}

# valid selection
selection.valid() {
  local word="${1:-}";
  if method.exists? selected; then
    selected "$@";
  fi
  method.remove selected;
  unset -v PROMPT_SELECTION;
  rlx.prompt;
}

# input error
selection.error() {
  local value="${1:-}";
  console error "invalid selection %s" "${value:-}";
}

# input range error
selection.range() {
  local value="${1:-}";
  console error "invalid selection %s, must be in the range %s-%s" \
    "${value}" "1" "${#PROMPT_SELECTION[@]}";
}

rlx.prompt.select() {
  prompt select --id=selection \
    --select=selection.valid \
    --select-error=selection.error \
    --select-range-error=selection.range;
}
