# shellcheck    shell=dash
___X_CMD_RAKU_LOADED=
___x_cmd_raku_loaded(){
    [ -n "$___X_CMD_RAKU_LOADED" ]
}

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

    if [ -z "$installable" ]; then
        ! ___x_cmd_raku_loaded || return
    else
        [ "installable" != "$___X_CMD_RAKU_LOADED" ] || return
    fi

    ___x_cmd_raku_load_checkenv || {
        x:warn "Cannot find raku installed in this current environment"
        ___x_cmd_raku_load_env
        return
    }

    if [ -z "$installable" ]; then
        ___X_CMD_RAKU_LOADED=1
    else
        ___x_cmd_python_load_checkinstallable || {
            x:warn "Current env is not installable"
            ___x_cmd_python_load_env
            return
        }
        ___X_CMD_RAKU_LOADED=installable
    fi

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

___X_CMD_RAKU_SHIMAPP_PATH=
___x_cmd_raku_load_env(){
    if [ -n "$___X_CMD_RAKU_SHIMAPP_PATH" ]; then
        return 0
    fi

    local x_=""
    ___x_cmd pkg sphere getbinpath_                 \
        "raku" "v2023.06" "bin"     || return

    ___X_CMD_RAKU_SHIMAPP_PATH="$x_"
    ___X_CMD_RAKU_LOADED=installable
}

# TODO: Add yes and quiet in the future
___x_cmd_raku_load_checkenv(){
    x hascmd raku   || return
}

___x_cmd_raku_load_checkinstallable(){
    [ -w "$(command -v raku)" ]
}
