from __future__ import annotations

import re

_AC_LABEL_RE = re.compile(
    r"^[\s>*_+-]*(?:\*\*)?\s*(AC|DOD)[-_ .]?(\d+)([a-zA-Z]?)\b", re.IGNORECASE
)

def canonical_ac_label(text: str) -> str:
    """``AC3`` / ``DOD2`` parsed from a criterion or a binding entry, or ``""``.

    Both sides of the join go through this one function, so a contract writing
    ``ac-3`` and a ticket writing ``**AC3**`` bind, and neither side can
    normalise differently from the other.
    """
    match = _AC_LABEL_RE.match(text.strip())
    if not match:
        return ""
    return f"{match.group(1).upper()}{int(match.group(2))}{match.group(3)}"
