import glob
import re

mapping = {
    r'\bQt\.ScrollBarAlwaysOff\b': 'getattr(getattr(Qt, "ScrollBarPolicy", Qt), "ScrollBarAlwaysOff")',
    r'\bQt\.CopyAction\b': 'getattr(getattr(Qt, "DropAction", Qt), "CopyAction")',
    r'\bQt\.CustomContextMenu\b': 'getattr(getattr(Qt, "ContextMenuPolicy", Qt), "CustomContextMenu")',
    r'\bQt\.LeftButton\b': 'getattr(getattr(Qt, "MouseButton", Qt), "LeftButton")',
    r'\bQt\.AlignCenter\b': 'getattr(getattr(Qt, "AlignmentFlag", Qt), "AlignCenter")',
    r'\bQtConst\.AlignCenter\b': 'getattr(getattr(QtConst, "AlignmentFlag", QtConst), "AlignCenter")',
    r'\bQt\.LeftDockWidgetArea\b': 'getattr(getattr(Qt, "DockWidgetArea", Qt), "LeftDockWidgetArea")',
    r'\bQt\.RightDockWidgetArea\b': 'getattr(getattr(Qt, "DockWidgetArea", Qt), "RightDockWidgetArea")',
    r'\bQt\.WindowMaximizeButtonHint\b': 'getattr(getattr(Qt, "WindowType", Qt), "WindowMaximizeButtonHint")',
    r'\bQt\.Horizontal\b': 'getattr(getattr(Qt, "Orientation", Qt), "Horizontal")',
    r'\bQt\.UserRole\b': 'getattr(getattr(Qt, "ItemDataRole", Qt), "UserRole")',
    r'\bQt\.ToolButtonTextBesideIcon\b': 'getattr(getattr(Qt, "ToolButtonStyle", Qt), "ToolButtonTextBesideIcon")',
    r'\bQt\.PointingHandCursor\b': 'getattr(getattr(Qt, "CursorShape", Qt), "PointingHandCursor")',
}

for py_file in glob.glob("*.py"):
    # Do not parse ourselves
    if py_file == "last_python_patch3.py" or py_file == "check_translations.py" or py_file == "patch_en_json.py":
        continue

    with open(py_file, 'r', encoding='utf-8') as f:
        content = f.read()

    new_content = content
    for pattern, replacement in mapping.items():
        new_content = re.sub(pattern, replacement, new_content)

    if new_content != content:
        with open(py_file, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"Patched {py_file}")

print("Done patching Qt constants for PyQt6 compatibility.")
