# shellcheck shell=dash

# @gemini -a file -t 0 -p
# Enter into REPL mode
# Input prompt
# Summarize
# Translate
# EXIT

# Envrionment ...
# ___X_CMD_CHAT_CURRENT_ATTCHMENT_LIST=""
# ___X_CMD_CHAT_CURRENT_SESSION=""
___x_cmd_chat___repl(){
    local session="$1"
    [ -z "$session" ] || set -- --session "$session"
    ___x_cmd_chat___exec --send "$@"
}


___x_cmd_chat___exec_repl(){
    local op="$1"
    [ "$#" -gt 0 ] || op=--app
    case "$op" in
        --start|--previous|--select|--app)
            shift; ___x_cmd_chat___exec_repl___"${op#--}" "$@" ;;
        *)  N=chat M="Not such option" log:ret:64 ;;
    esac
}

___x_cmd_chat___exec_repl___app(){
    ___x_cmd_runmode_allow_manual || N=chat M="Repl can only be used in an interactive environment" log:ret:1
    if [ -z "$session" ]; then
        local x_="";local msg="";local previous=""
        ! ___x_cmd_chat___exec_repl___id_previous_ || previous="$x_"
        ___x_cmd ui select ,msg     "NEXT"                      \
                "start a new repl"                              \
                ${previous:+"using previous repl -> $previous"} \
                "interactive Selection Previous repl"           \
                "return 0"
        case "$msg" in
            start*)     ___x_cmd_chat___exec_repl___start       "$@" ;;
            using*)     ___x_cmd_chat___exec_repl___previous    "$@" ;;
            interactive*)
                        ___x_cmd_chat___exec_repl___select      "$@" ;;
            *)          return 0 ;;
        esac
    else
        ___x_cmd_chat___exec_repl_loop "$@"
    fi
}

___x_cmd_chat___exec_repl___start(){
    local x_=""; ___x_cmd_chat___exec_repl___id_ || return $?
    local session="$x_"
    ___x_cmd_chat___exec_repl_loop "$@"
}

___x_cmd_chat___exec_repl___previous(){
    local x_=""; ___x_cmd_chat___exec_repl___id_previous_ || return $?
    local session="$x_"
    ___x_cmd_chat___exec_repl_loop "$@"
}

___x_cmd_chat___exec_repl___select(){
    local x_=""; ___x_cmd_chat___exec_repl___id_previous_app_ || return $?
    local session="$x_"
    ___x_cmd_chat___exec_repl_loop "$@"
}

# x ls | @ -a 'work.note' --> Save to the llm results ...

___x_cmd_chat___exec_repl_loop(){
    trap ':' INT
    chat:info "repl [session=$session]"

    local history_num="${history_num:-10}"
    local ___X_CMD_MD_LLM_LESS_DISABLE=1
    local x_=""
    local question="$*"

    x_=; ___x_cmd_chat___exec_repl___session_runid_ "$session"
    local run_id="$x_";

    while true; do
        printf "[%s] %s\n" "$session"  "$run_id" >"$___X_CMD_DEVTTY_FP"
        x_=; ___x_cmd_chat___exec_repl___ted_ "$question" || break
        printf "\033[1;7m>>> %s \033[0m\n" "$x_" > "$___X_CMD_DEVTTY_FP"

        ___x_cmd_chat___exec_repl____handlecmd_

        case "$x_" in
            EXIT)       break ;;
            CONT)       continue ;;
        esac
    done

    # store the default session value ...
    chat:info \
        --tip1 "resume the session -> @ -s $session" \
        --tip2 "resume the session -> @ -s -" \
        "Exit Session -> $session"
}

# show all of the help ...
___x_cmd_chat___exec_repl___printhelp(){
    ___x_cmd ui style info "Commands:
    /h|/help    | show help
    /exit|/quit | exit the loop
    /history    | manage repl history records
    = <command> | execute the shell command

Other inputs continue the loop, use \ to start multi-line input content
" >"$___X_CMD_DEVTTY_FP"
}

___x_cmd_chat___exec_repl___nexthelp(){
    local id=""
    ___x_cmd ui select id   "Next"                  \
        "/h         | show help"                    \
        "/exit      | exit the loop"                \
        "/history   | manage repl history records"  \
        "...        | continue the loop"
    case "$id" in
        1)      ___x_cmd_chat___exec_repl___printhelp ;;
        2)      x_=EXIT;    return 0 ;;
        3)      ___x_cmd_chat___exec_repl_history app ;;
        4)      x_=CONT;    return 0 ;;
    esac
}

___x_cmd_chat___exec_repl___slash_check(){
    local str="$1"
    case "$str" in
        /h|/help|/exit|/quit|/history) return 0;;
        *) return 1 ;;
    esac
}

___x_cmd_chat___exec_repl____handlecmd_(){
    case "$x_" in
        # /attach)   add attachment to the list ;
        # /rm)       remove attachment from the list ;;

        /h|/help)       ___x_cmd_chat___exec_repl___printhelp ;;
        /exit|/quit)    x_=EXIT;    return 0 ;;
        /init)          ___x_cmd_chat_init      ;;
        /history)       ___x_cmd_chat___exec_repl_history app   ;;
        "")             ___x_cmd_chat___exec_repl___nexthelp; return ;;

        \=' '*)
                        chat:info "Execute the command —> ${x_#'= '}"
                        eval "${x_#'= '}"
                        ;;
        *)
                        ___x_cmd_chat___exec_repl_history add "$x_"
                        is_silent=1
                        ___x_cmd_chat___exec_raw "$x_" "$history_num" || {
                            chat:debug "Execution failed"
                            x_=CONT;    return 0
                        }
                        run_id=$(( run_id + 1 ))
    esac

    x_=""
}
