import re

patterns = {
    "console_log": re.compile(r"console\.log\s*\(\s*['\"]\[[\w_]+\]\[[A-Za-z0-9_:]+\]"),
    "js_anchor_start": re.compile(r"//\s*\[DEF:(?P<name>[\w\.]+):(?P<type>\w+)\]"),
    "js_anchor_end": re.compile(r"//\s*\[/DEF:(?P<name>[\w\.]+)(?::\w+)?\]"),
    "html_anchor_start": re.compile(r"<!--\s*\[DEF:(?P<name>[\w\.]+):(?P<type>\w+)\]\s*-->"),
    "html_anchor_end": re.compile(r"<!--\s*\[/DEF:(?P<name>[\w\.]+)(?::\w+)?\]\s*-->"),
}

stack = []
with open("frontend/src/lib/components/assistant/AssistantChatPanel.svelte") as f:
    for i, line in enumerate(f):
        line_stripped = line.strip()
        m_start = patterns["html_anchor_start"].search(line_stripped) or patterns["js_anchor_start"].search(line_stripped)
        if m_start:
            stack.append(m_start.group("name"))
        
        m_end = patterns["html_anchor_end"].search(line_stripped) or patterns["js_anchor_end"].search(line_stripped)
        if m_end:
            stack.pop()

        if patterns["console_log"].search(line):
            print(f"Matched console.log on line {i+1} while stack is {stack}")

