# kairos completion for bash: eval "$(kairos --completion bash)"
_kairos()
{
  local cur="${COMP_WORDS[COMP_CWORD]}" prev=""
  (( COMP_CWORD > 0 )) && prev="${COMP_WORDS[COMP_CWORD-1]}"
  COMPREPLY=()
  local opt="$prev" eq=0 pre=""
  if [[ "$cur" == "=" ]]; then
    cur=""; eq=1
  elif [[ "$prev" == "=" ]] && (( COMP_CWORD > 1 )); then
    opt="${COMP_WORDS[COMP_CWORD-2]}"; eq=1
  elif [[ "$cur" == --*=* ]]; then
    opt="${cur%%=*}"; pre="$opt="; cur="${cur#*=}"; eq=1
  fi
  case "$opt" in
    --completion)
      COMPREPLY=($(compgen -W "bash zsh fish" -P "$pre" -- "$cur")); return 0;;
    --engine)
      COMPREPLY=($(compgen -W "auto m1 m2 m3" -P "$pre" -- "$cur")); return 0;;
    --format)
      COMPREPLY=($(compgen -W "ipc popf json" -P "$pre" -- "$cur")); return 0;;
    --search)
      COMPREPLY=($(compgen -W "auto ff lama eager-lama novelty lama-novelty ehc goals" -P "$pre" -- "$cur")); return 0;;
    --debug)
      if (( eq )); then
        local done=""
        [[ "$cur" == *,* ]] && done="${cur%,*},"
        COMPREPLY=($(compgen -W "all general parse semantic normalize ground sas search heuristic temporal postprocess validate orchestrator memory" -P "$pre$done" -- "${cur##*,}"))
        compopt -o nospace 2>/dev/null
        return 0
      fi;;
    --dump)
      if (( eq )); then
        local done=""
        [[ "$cur" == *,* ]] && done="${cur%,*},"
        COMPREPLY=($(compgen -W "ast lifted ground sas plan-trace" -P "$pre$done" -- "${cur##*,}"))
        compopt -o nospace 2>/dev/null
        return 0
      fi;;
    --output|--problem)
      compopt -o filenames 2>/dev/null
      COMPREPLY=($(compgen -f -- "$cur")); return 0;;
    --dump-dir)
      compopt -o filenames 2>/dev/null
      COMPREPLY=($(compgen -d -- "$cur")); return 0;;
    --timeout|--memory|--threads|--epsilon|--seed|--target-cost|--no-improvement)
      return 0;;
  esac
  local command=""
  (( COMP_CWORD > 1 )) && command="${COMP_WORDS[1]}"
  case "$command" in
    solve|validate|check) ;;
    *) command="solve";;
  esac
  if [[ "$cur" == -* ]]; then
    local options=""
    (( COMP_CWORD == 1 )) && options="--help --version --completion --features"
    case "$command" in
      solve) options="$options --timeout --engine --format --output --memory --threads --epsilon --seed --target-cost --no-improvement --dump-dir --search --debug --dump --anytime --no-anytime -v";;
      validate) options="$options --epsilon";;
      check) options="$options --problem";;
    esac
    COMPREPLY=($(compgen -W "$options" -- "$cur"))
    [[ ${#COMPREPLY[@]} -eq 1 && "${COMPREPLY[0]}" == *= ]] && compopt -o nospace 2>/dev/null
    return 0
  fi
  (( COMP_CWORD == 1 )) && COMPREPLY=($(compgen -W "solve validate check" -- "$cur"))
  compopt -o filenames 2>/dev/null
  local IFS=$'\n'
  local files=($(compgen -f -X '!*.pddl' -- "$cur") $(compgen -d -- "$cur"))
  [[ ${#files[@]} -eq 0 ]] && files=($(compgen -f -- "$cur"))
  COMPREPLY+=("${files[@]}")
  return 0
}
complete -F _kairos kairos
