# shellcheck shell=dash

x log init ollama

xrc:mod:lib     ollama   \
    install uninstall util cfg  \
    ddgo ls info ps run pull la

if ___x_cmd_is_suitable_advise_env; then
    xrc:mod:lib     ollama    advise
fi


___x_cmd_ollama___main(){
    [ "$#" -gt 0 ] ||   set - ls

    local op="$1"; shift
    case "$op" in
        cfg|--cfg)  ___x_cmd_ollama_cfg             "$@" ;;
        api)    ;;

        :|ddgo)     ___x_cmd_ollama_ddgo            "$@" ;;
        ::|ddai)    ___x_cmd_ollama_ddai            "$@" ;;

        install|uninstall)
                        "___x_cmd_ollama___${op}"   "$@" ;;

        run|info|ls|browse|chat|init|ps|la|pull)
                        ___x_cmd_ollama_"$op"       "$@" ;;

        -h|--help)      ___x_cmd help -m ollama     "$@" ;;
        --hascmd)       ___x_cmd_ollama___hascmd    "$@" ;;

        *)              ___x_cmd_ollama___bin "$op" "$@" ;;
    esac
}

___x_cmd_ollama___bin(){
    ___x_cmd_ollama_is_installed || return $?

    ___x_cmd_ollama___bin(){
        ___x_cmd_cmds ollama "$@"
    }

    ___x_cmd_ollama___bin "$@"
}

___x_cmd_ollama___hascmd(){
    ___x_cmd hascmd ollama || {
        ollama:error --cmd 'x install ollama' "Ollama is not installed, please install it first"
        return 1
    }
}

___x_cmd_ollama_browse(){
    local id="${1%%:*}"
    ollama:info "running -> x ollama browse $id"
    ___x_cmd browse "https://ollama.com/library/$id"
}

