def _build_rules_helper(search):
    """Helper function for build_rules().

    A branch node like:

        ["and", [node1, node2]]

    will be transformed, recursively, into:

        {
            "condition": "AND",
            "rules": [_build_rules_helper(node1), _build_rules_helper(node2)]
        }

    A leaf node like:

        ["nm", "contains", "paracetamol"]

    will be transformed into:

        {"id": "nm", "operator": "contains", "value": "paracetamol"}
    """

    assert len(search) in [2, 3]

    if len(search) == 2:
        # branch node
        return {
            "condition": search[0].upper(),
            "rules": list(map(_build_rules_helper, search[1])),
        }
    else:
        # leaf node
        value = search[2]
        if value is True:
            value = 1
        elif value is False:
            value = 0

        return {"id": search[0], "operator": search[1], "value": value}