from typing import List
import string
import collections


def sanitize_header(labels: List[str]):
    """Sanitize header labels."""
    # Handle Treat Empty Header
    for idx, label in enumerate(labels):
        if label.strip():
            # make it lowercase
            label = label.lower()

            # remove punctuations
            label = ''.join([l for l in label if l not in string.punctuation])  # noqa: E741

            # replace spaces with underscores
            label = label.replace(" ", "_")
        else:
            label = f"col_{idx}"
        labels[idx] = label

    # Deduplicate by adding _1, _2, _3 to repeated labels
    counts = {k: v for k, v in collections.Counter(labels).items() if v > 1}
    for i in reversed(range(len(labels))):
        item = labels[i]
        if item in counts and counts[item]:
            labels[i] = f"{item}_{counts[item]}"
            counts[item] -= 1

    return labels