import re
from pathlib import Path

TARGET_MAP: dict[Path, Path] = {
    Path("hllrcon/commands.py"): Path("hllrcon/sync/commands.py"),
    Path("tests/test_commands.py"): Path("tests/test_sync_commands.py"),
}

REPLACE_MAP = {
    r"hllrcon\.commands": "hllrcon.sync.commands",
    r"RconCommands": "SyncRconCommands",
    r"async def": "def",
    r"await ": "",
    r"asyncio.gather": "",
    r"Coroutine\[Any, Any, (\w+)\]": r"\g<1>",
    r"pytestmark = pytest\.mark\.asyncio": "",
}

ASCII_HEADER = """######################################################
#  Generated using `scripts/generate_sync_commands.py`
#  DO NOT EDIT THIS FILE DIRECTLY
######################################################

"""


def main() -> None:
    for src_path, out_path in TARGET_MAP.items():
        src = src_path.read_text(encoding="utf-8")
        for old, new in REPLACE_MAP.items():
            src = re.sub(old, new, src)
        out_path.write_text(ASCII_HEADER + src, encoding="utf-8")

        src = ASCII_HEADER + src
        out_path.write_text(src, encoding="utf-8")


if __name__ == "__main__":
    main()
