# shellcheck shell=dash

___x_cmd_node_co(){
    [ "$#" -gt 0 ] || set -- "$(cat)"
    ___x_cmd chat --request --type  co,node --minion "$___X_CMD_ROOT_MOD/node/lib/minion/co.yml" "$@"
}

# using the node facility to test the code

___x_cmd_node_coco(){
    [ "$#" -gt 0 ] || set -- "$(cat)"
    # Consider refine the code ...
    code=$(___x_cmd chat --request --type coco,node --minion "$___X_CMD_ROOT_MOD/node/lib/minion/coco.yml" "$@")

    local feedback
    feedback=$(printf "%s\n" "$code" | ___x_cmd node --check 2>&1) || {
        {
            printf "Linting fail. Here is the feedback:"
            printf "%s\n" "$feedback"
        } >/dev/tty

        while true; do
            ___x_cmd ui select "" "what's next" "Send feedback to ai for better answer" "view the feed back and code" "Exit" || {
                co:error "Recv interrupts. Exit"
                return 130
            }
            case "$___X_CMD_UI_SELECT_RET" in
                1)
                    # ___x_cmd chat -n 0 --request coco,node "$feedback" "$msg"
                    #
                    x:error "Unsuported"
                    return 1
                    ;;
                2)  printf "%s\n" "$code" | ___x_cmd bat -l node ;;
                3)  return 1 ;;
            esac
        done
    }

    printf "%s\n" "$code" | ___x_cmd bat -l js
    ___x_cmd ui yesno "Do you want to run the code" || return 0        # default is no.

    printf "%s\n" "$code" | ___x_cmd node
}
