from __future__ import annotations

import re
from typing import Any


def parse_device_command(text: str) -> dict[str, Any] | None:
    """解析 /device 指令，如 `/device open_app Safari`。"""
    stripped = text.strip()
    if not stripped.startswith("/device"):
        return None
    rest = stripped.removeprefix("/device").strip()
    if not rest:
        return None
    parts = rest.split(maxsplit=1)
    action_type = parts[0]
    arg = parts[1].strip() if len(parts) > 1 else ""
    if action_type == "open_app":
        return {"type": "open_app", "params": {"app": arg}}
    if action_type == "run_shortcut":
        return {"type": "run_shortcut", "params": {"shortcut": arg}}
    if action_type in ("open", "app"):
        return {"type": "open_app", "params": {"app": arg}}
    return {"type": action_type, "params": {"raw": arg}}
