# shellcheck shell=dash

___x_cmd_xx___co_fix_(){
    local _stderrfp=
    local _cmdstr=
    local localshell=
    local os=
    local op="$1";  shift
    case "$op" in
        --cmdstr)       _stderrfp="$1";     _cmdstr="$2"; shift 2 ;;
        --cmdarr)       _stderrfp="$1";
                        shift 1
                        ___x_cmd cmdstr _cmdstr "$@"
                        ;;
        *)              N=xx M="Not support such subcmd[$op]" log:ret:64
                        ;;
    esac

    [ -n "$_cmdstr" ] || {
        xx:error "The shell command to fix is null"
        return 1
    }

    # provide command version
    # provide os version
    # provide help info ...

    # RERUN_RESULT

    # provide more ...
    x_=; [ ! -f "$_stderrfp" ] || ___x_cmd rat_ "$_stderrfp" || return
    local _rerun_result="$x_"

    ___x_cmd os name_  || return
    local _os_name="$x_"

    local ROOT_USER="You login as a non-root user."
    if ___x_cmd user isroot; then
        ROOT_USER="Now you login in as a root user."
    fi

    xx:info "Requesting ai to fix the [cmd=$_cmdstr]"
    x_="$(
        ___X_CMD_OS_NAME_="$_os_name" \
        ___X_CMD_SHELL="$___X_CMD_SHELL" \
        ___X_CMD_XX_RERUN_RESULT="$_rerun_result" \
        ROOT_USER="$ROOT_USER" \
        ___x_cmd chat --request --type "xx" --minion "$___X_CMD_ROOT_MOD/xx/lib/fix/minion/co.yml" "$_cmdstr" | ___x_cmd jo t " "
    )" || return

    xx:debug --content "$x_" "AI fix response"

    case "$x_" in
        NONE*|'[ ] '|'{ } ')
            xx:warn "AI model fails to fix the command or guess your intention."
            # xx:info "Could you be more specific and provide more information ?" # co
            return 1
        ;;
        '['*)  ;;
        *) x_="[ ${x_} ]";;
    esac
}
