from alaya.packages.eth_utils import (
    is_dict,
)

from alaya.datastructures import (
    AttributeDict,
)
from alaya.utils.toolz import (
    assoc,
)


def attrdict_middleware(make_request, web3):
    """
    Converts any result which is a dictionary into an a
    """
    def middleware(method, params):
        response = make_request(method, params)

        if 'result' in response:
            result = response['result']
            if is_dict(result) and not isinstance(result, AttributeDict):
                return assoc(response, 'result', AttributeDict.recursive(result))
            else:
                return response
        else:
            return response
    return middleware
