"""Orbe recipe — scroll a list and collect its items.

Reading the page (text and actionable elements) is done in the renderer through
the channel — no script runs in the page, no CDP domain is enabled. `scroll_until`
scrolls with real human gestures until the target text appears or the page bottom
is reached (the distance comes from a human recording, not a fixed pixel amount).

Run:
    export ORBE_PROFILE=my-profile-id
    python examples/scrape_list.py
"""
import os

import orbe


def main() -> int:
    profile = os.environ["ORBE_PROFILE"]
    seen: set[str] = set()

    with orbe.Trinh(profile) as t:
        t.goto("https://example.com/list")
        t.wait_for("Load more")

        # Scroll a few pages, collecting link items as they appear.
        for _ in range(5):
            for el in t.elements():             # EN keys: {type, text, link, x, y, w, h}
                if el.get("type") == "link" and el.get("text"):
                    seen.add(el["text"].strip())
            # Stop early once we reach a marker near the end of the list.
            if not t.scroll_until("End of results", max_gestures=1):
                t.scroll(900)                   # one human scroll gesture, then loop
            else:
                break

        for item in sorted(seen):
            print(item)
        print(f"\nCollected {len(seen)} item(s).")
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except orbe.Blocked as e:
        print(f"Blocked: {e}")
        raise SystemExit(2)
