from typing import Callable, Any

from rx import Observable

from interstate_py.interstate_server import InterstateServer
from interstate_py.zeromq.aio_zmq_server import AIOZMQServer


class Interstate:
    """
    The main entry point for the interstate server
    """

    @staticmethod
    def transformer_server(transformer: Callable[[Observable], Observable], port: int = 8077) -> InterstateServer:
        server = AIOZMQServer(port, transformer)
        server.start_server()

        return server

    @staticmethod
    def request_response_server(req_res_handler: Callable[[Any], Any], port: int = 8077) -> InterstateServer:
        def transformer(obs: Observable) -> Observable:
            return obs.map(req_res_handler)

        server = AIOZMQServer(port, transformer)
        server.start_server()

        return server

    @staticmethod
    def request_stream(req_stream: Callable[[Any], Observable], port: int = 8077) -> InterstateServer:
        def transformer(obs: Observable) -> Observable:
            return obs.flat_map(req_stream)

        server = AIOZMQServer(port, transformer)
        server.start_server()

        return server
