# shellcheck shell=dash

# TODO:
# browse pdf <website>
# browse install-chromium <website>
# browse ... <website>
# browse config -- open config

x log init browse

___x_cmd_browse___main(){
    [ "$#" -gt 0 ] || {
        ___x_cmd_browse_help
        return 64
    }

    local op="$1"; shift
    case "$op" in
        open|config)
                    "___x_cmd_browse_${op}" "$@" ;;
        *)          ___x_cmd_browse_open "$op" "$@"
    esac
}

# system browser
# default browser
# google chrome
# firefox
# chromium

# TODO: in the future, we will open the default browswer, instead of opening the system browswer.
# We are using open module to replace it.
___x_cmd_browse_open() {
    local ___X_CMD_BROWSE_BROWSER="${___X_CMD_BROWSE_BROWSER:-$___X_CMD_BROWSE_BROWSER}"
    local website="${1:?Provide websites}"
    if [ -n "$___X_CMD_BROWSE_BROWSER" ]; then
        "$___X_CMD_BROWSE_BROWSER" "$website"
        return
    fi

    local edge_path="/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"

    case "$website" in
        http*://*)  ;;
        ftp*://*)   ;;
        *)  website="http://$website"
    esac

    case "$(___x_cmd os name)" in
        darwin)
                # open -a "/Applications/Safari.app" "$website" ;
                open "$website"
                return ;;
        win*)
                "$edge_path" "$website";
                return ;;
    esac

    local edge_path_mnt="/mnt$edge_path"
    if [ -f "$edge_path_mnt" ]; then
        "$edge_path_mnt" "$website"
    elif command -v xdg-open >/dev/null; then
        command xdg-open "$website"
    elif command -v gnome-open >/dev/null; then
        command gnome-open "$website"
    else
        # Check wether
        browse:debug "Could not detect the web ___X_CMD_BROWSE_BROWSER to use."
        return 1
    fi
}
