"""
Demo: Iterate over raw pywasot events.

This demo shows how to use the raw Rust types directly from pywasot._pywasot
for maximum performance and minimal overhead.
"""
from pywasot._pywasot import (
    BlockStartedEvent,
    BlockFinishedEvent,
    TextEvent,
    ContextLineEvent,
    VoidElementEvent,
    parse_wasot_string,
)

content = """.document
.section :intro
..heading .. Introduction
..paragraph
This is the first paragraph.
It has multiple lines.
..paragraph
This is the second paragraph.
.section :main
..heading .. Main Content
..list
...item
Item one
...item
Item two
"""


indent = 0
for event in parse_wasot_string(content, None):
    if isinstance(event, BlockStartedEvent):
        meta = event.meta_line
        print(f"{'  '*indent}BlockStarted: .{meta.type_name} (level={meta.level})")
        if meta.inline_text.content:
            print(f"{'  '*indent}  inline_text: {meta.inline_text.content}")
        if meta.arguments.args:
            args = [a.content for a in meta.arguments.args]
            print(f"{'  '*indent}  args: {args}")
        if meta.block_id:
            print(f"{'  '*indent}  id: {meta.block_id}")
        indent += 1

    elif isinstance(event, TextEvent):
        lines_preview = event.lines[:2]
        more = "..." if len(event.lines) > 2 else ""
        print(f"{'  '*indent}TextEvent: {lines_preview}{more}")

    elif isinstance(event, VoidElementEvent):
        meta = event.meta_line
        print(f"{'  '*indent}VoidElement: .{meta.type_name}")

    elif isinstance(event, ContextLineEvent):
        meta = event.meta_line
        print(f"{'  '*indent}ContextLine: ./{meta.type_name}")

    elif isinstance(event, BlockFinishedEvent):
        indent = max(0, indent - 1)
        print(f"{'  '*indent}BlockFinished")