# shellcheck shell=dash

___X_CMD_PYTHON_LOADED=
___x_cmd_python_loaded(){
    [ -n "$___X_CMD_PYTHON_LOADED" ]
}

___x_cmd_python_load(){
    local installable=
    if [ "$1" = "--installable" ]; then
        installable=1
    fi

    if [ -z "$installable" ]; then
        ! ___x_cmd_python_loaded || return 0
    else
        [ "installable" != "$___X_CMD_PYTHON_LOADED" ] || return
    fi

    ___x_cmd_python_load_checkenv || {
        python:debug "Cannot find python installed in this current environment"
        ___x_cmd snap --run python3 --version >/dev/null 
        return
    }

    if [ -z "$installable" ]; then
        ___X_CMD_PYTHON_LOADED=1
    else
        ___x_cmd_python_load_checkinstallable || {
            python:warn "Current env is not installable"
            ___x_cmd snap --run python --version >/dev/null
            return
        }
        ___X_CMD_PYTHON_LOADED=installable
    fi

    # TODO: In the future, we need to check the current version is good enough.
}

# TODO: Add yes and quiet in the future
___x_cmd_python_load_checkenv(){
    ___x_cmd_hasbin python3 && ___x_cmd_hasbin pip3  || return

}

___x_cmd_python_load_checkinstallable(){
    # [ -w "$(command -v python3)" ]
    # [ -w "$___X_CMD_PYTHON_SHIMAPP_PATH" ]

    local x_
    ___x_cmd_whichbin_ pip3 || return
    [ -w "$x_" ]
}
