"""Shared CSS custom property parser for insane-design scripts."""

from __future__ import annotations

import re


def parse_all_custom_properties(css: str) -> dict[str, str]:
    """Return {--name: raw_value} for all CSS custom property declarations.

    - Strips CSS comments before parsing
    - Uses [^;{}]+ to prevent matching across block boundaries
    - Last-wins for duplicate keys (CSS cascade behavior)
    """
    cleaned = re.sub(r"/\*.*?\*/", "", css, flags=re.S)
    props: dict[str, str] = {}
    for match in re.finditer(r"--([\w-]+)\s*:\s*([^;{}]+)", cleaned):
        props[f"--{match.group(1)}"] = match.group(2).strip()
    return props
