from typing import Counter


def _name_steps(steps):
    """Generate names for steps."""
    # Based on sklearn.pipeline._name_estimators

    steps = list(reversed(steps))

    names = [type(step).__name__.lower() for step in steps]

    name_count = {k: v for k, v in Counter(names).items() if v > 1}

    named_steps = []
    for name, step in zip(names, steps):
        count = name_count.get(name, 0)
        if count:
            name_count[name] = count - 1
            name = f"{name}_{count}"

        named_steps.append((name, step))

    named_steps.reverse()

    return named_steps