# shellcheck shell=dash

xrc:mod:lib     whisper     dictate/util

___x_cmd_whisper___dictate(){
    local modelName=""
    local output=""
    local input=""

    local ___X_CMD_WHISPER_MAIN_OPTS=''

    while [ $# -gt 0 ];do
        case "${1}" in
            -h|--help)
                ___x_cmd help -m whisper dictate
                return
                ;;
            --json-full|--jsonfull)
                ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS "--output-json-full"
                shift
                continue
                ;;
            --karaok)
                ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS "--output-words"
                shift
                continue
                ;;
            --csv|--srt|--txt|--json)
                ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS "--output-${1#--}"
                shift
                continue
                ;;

            -m|--model)
                modelName="${2}"
                [ $# -ge 2 ] || N=whisper M="Please provide model name"  log:ret:64
                ;;
            -f|--file)
                input="${input}${2}${___X_CMD_UNSEENCHAR_NEWLINE}"
                [ $# -ge 2 ] || N=whisper M="Please provide input file path"  log:ret:64
                ;;
            -o|-of|--output-file)
                output="${output}${2}${___X_CMD_UNSEENCHAR_NEWLINE}"
                [ $# -ge 2 ] || N=whisper M="Please provide output file path"  log:ret:64
                ;;

            -sow|--split-on-word|\
            -debug|--debug-mode|\
            -tr|--translate|\
            -di|--diarize|\
            -tdrz|--tinydiarize|\
            -nf|--no-fallback|\
            -otxt|--output-txt|\
            -ovtt|--output-vtt|\
            -osrt|--output-srt|\
            -olrc|--output-lrc|\
            -owts|--output-words|\
            -ocsv|--output-csv|\
            -oj|--output-json|\
            -ojf|--output-json-full|\
            -ps|--print-special|\
            -pc|--print-colors|\
            -pp|--print-progress|\
            -nt|--no-timestamps|\
            -dl|--detect-language|\
            -ls|--log-score|\
            -ng|--no-gpu)
                ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS "$1";
                shift;
                continue
                ;;

            -*)
                ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS "$1" "$2"
                [ $# -ge 2 ] || N=whisper M="Please provide a value"  log:ret:64
                ;;
            *)  break ;;
        esac
        shift 2
    done

    local x_=""
    [ -n "${modelName}" ] || {
        if ___x_cmd_is_stdout2tty &&  ___x_cmd_runmode_allow_chatty; then
            ___x_cmd_whisper___dictate___decidemodel_ || return 1
            modelName="${x_}"
        else
            N=whisper M="modelName is empty" log:ret:1
        fi
    }

    [ -n "${modelName}" ] || N=whisper M="model is empty" log:ret:1
    ___x_cmd_whisper_model_default set "$modelName"

    [ "$1" != -- ] || shift

    while [ "$#" -gt 0 ]; do
        input="${input}${1}${___X_CMD_UNSEENCHAR_NEWLINE}"
        shift
    done

    [ -n "$input" ] || N=whisper M="no input files specified" log:ret:1
    local il="$input"
    local ie=""

    local ol="$output"
    local oe=

    local input_file_wav=""
    while [ "$il" != "" ]; do
        ie="${il%%"${___X_CMD_UNSEENCHAR_NEWLINE}"*}"

        if [ "$ie" = "$il" ]; then
            il=""
        else
            il="${il#"${ie}"?}"
        fi

        [ "$ie" != "" ] || break
        [ -f "$ie" ] || N=whisper M="File No Exists -> $ie" log:ret:64

        x_=; ___x_cmd_whisper_prepare_ "$ie" || return
        input_file_wav="$x_"

        oe="${ol%%"${___X_CMD_UNSEENCHAR_NEWLINE}"*}"
        ol="${ol#"${oe}"?}"
        [ -n "$oe" ] || oe="${ie%.*}"
        ___x_cmd ensurefp "$oe"
        ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS --output-file "$oe" "$input_file_wav"
    done

    if [ -f "$modelName" ]; then        x_="$modelName"
    else                                x_="";  ___x_cmd_whisper_model_download_ "${modelName}"
    fi

    ___x_cmd cmdstr ___X_CMD_WHISPER_MAIN_OPTS --model "$x_"

    eval set -- ___x_cmd whispercpp "$___X_CMD_WHISPER_MAIN_OPTS" "\"\$@\""
    local IFS=" "; whisper:debug "cmd -> $*";   "$@"
}
