#!/usr/bin/env python3
"""Regenerate the Rust expert hotlists from a DS4 source checkout."""

import argparse
import pathlib
import re


ARRAY = re.compile(
    r"static const uint16_t ds4_default_streaming_hotlist_(\w+)\[\]\[2\] = \{(.*?)\n\};",
    re.DOTALL,
)
PAIR = re.compile(r"\{(\d+),\s*(\d+)\}")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("source", type=pathlib.Path)
    parser.add_argument("output", type=pathlib.Path)
    args = parser.parse_args()
    texts = [
        (args.source / "ds4_streaming_hotlist.inc").read_text(),
        (args.source / "ds4_streaming_hotlist_glm52.inc").read_text(),
    ]
    arrays = {
        name: PAIR.findall(body)
        for text in texts
        for name, body in ARRAY.findall(text)
    }
    names = (("PRO", "pro"), ("FLASH", "flash"), ("GLM52", "glm52"))
    lines = ["// Generated mechanically by scripts/import_hotlists.py.\n"]
    for constant, source_name in names:
        lines.append(f"pub(super) const {constant}: &[(u16, u16)] = &[\n")
        lines.extend(f"    ({layer}, {expert}),\n" for layer, expert in arrays[source_name])
        lines.append("];\n")
    args.output.write_text("".join(lines))


if __name__ == "__main__":
    main()
