from typing import Any, Callable, Union

from .. import globals  # pylint: disable=redefined-builtin


def open(target: Union[Callable[..., Any], str], new_tab: bool = False) -> None:  # pylint: disable=redefined-builtin
    """Open

    Can be used to programmatically trigger redirects for a specific client.

    Note that *all* clients (i.e. browsers) connected to the page will open the target URL *unless* a socket is specified.
    User events like button clicks provide such a socket.

    :param target: page function or string that is a an absolute URL or relative path from base URL
    :param new_tab: whether to open the target in a new tab
    """
    path = target if isinstance(target, str) else globals.page_routes[target]
    client = globals.get_client()
    if client.has_socket_connection:
        client.open(path, new_tab)
    else:
        globals.log.error('Cannot open page because client is not connected, try RedirectResponse from FastAPI instead')
