from typing import Callable


def apply_on_each_term(query: str, function: Callable) -> str:
    """
    Apply a function to each term of the query

    Parameters
    ----------
    query : str
        A search query
    function : Callable
        A callable function that will be applied to each term of the provided query

    Returns
    -------
    str
        A new query with each term transformed by the provided function
    """

    is_inside_a_term = False
    search_term = ''
    final_query = ''
    for character in query:

        if character == '[':
            search_term += character
            is_inside_a_term = True
            continue

        if is_inside_a_term:
            search_term += character
            if character == ']':
                search_term = function(search_term)
                final_query += search_term
                search_term = ''
                is_inside_a_term = False
        else:
            final_query += character

    return final_query