import textwrap


def construct_degree_clause(metarels):
    """
    Create a Cypher query clause that calculates the degree of each node

    Parameters
    ----------
    metarels : a metarels or MetaPath object
        the metapath to create the clause for
    """

    degree_strs = list()
    for i, (source_label, target_label, rel_type, direction) in enumerate(metarels):
        kwargs = {
            "i0": i,
            "i1": i + 1,
            "source_label": source_label,
            "target_label": target_label,
            "rel_type": rel_type,
            "dir0": "<-" if direction == "backward" else "-",
            "dir1": "->" if direction == "forward" else "-",
        }
        degree_strs.append(
            textwrap.dedent(
                """\
            size((n{i0}){dir0}[:{rel_type}]{dir1}()),
            size((){dir0}[:{rel_type}]{dir1}(n{i1}))"""
            ).format(**kwargs)
        )
    degree_query = ",\n".join(degree_strs)

    return degree_query