
    i                     X    d Z ddlZddlZddlZddlmZmZ ddlmZ  G d d          Z	dS )z
Scroll Page Primitive Operation

This module provides page scrolling functionality for browser automation.
Simulates human-like scrolling behavior with natural overshooting and recovery.
    N)DictAny)Pagec                   V    e Zd ZdZd Zddedededeee	f         fdZ
dededefd	Zd
S )
ScrollPagea}  
    Scroll Page with Human-like Behavior

    Implements realistic scrolling behavior:
    - Overshoots the target distance for natural feel
    - Mouse movement during scroll
    - Slight scroll back (overshoot recovery)
    - Natural delays between actions

    Usage:
        scroller = ScrollPage()
        result = await scroller.scroll(page, distance=1000, slowmo=1000)
    c                 $    t          d           dS )z#Initialize the scroll page handler.u   📜 ScrollPage initializedN)print)selfs    >/home/byschii/byschiidev/penelope/lib/primitive/scroll_page.py__init__zScrollPage.__init__   s    +,,,,,      pagedistanceslowmoreturnc                   K   	 t          d| d           |t          j        dd          z  }|                     ||           d{V  t	          j        d           d{V  |j                            t          j        dd          t          j        dd          d	
           d{V  |                     || t          j        dd          z             d{V  t	          j        d           d{V  t	          j        |dz             d{V  d||j	        d}t          d|dd           |S # t          $ r0}dt          |          d}t          d|            |cY d}~S d}~ww xY w)a|  
        Scroll the page vertically by a specified distance.

        Args:
            page (Page): Playwright page to use
            distance (int): Distance in pixels to scroll down (default: 1000)
            slowmo (int): Delay in milliseconds for waiting after scroll (default: 1000)

        Returns:
            Dict containing scroll result and page information
        u   📜 Scrolling page by z pixelsg?g333333?Ng      ?d   i,     )stepsg?g?r   T)successscrolled_distanceurlu   ✅ Page scrolled by z.0fF)r   erroru   ❌ Page scroll failed: )r	   randomuniform_human_scrollasynciosleepmousemoverandintr   	Exceptionstr)r
   r   r   r   actual_distanceresulteerror_results           r   scrollzScrollPage.scroll#   s     !	 =H===>>> 'S)A)AAO$$T?;;;;;;;;;-$$$$$$$$$ *//&.c":":FN3PS<T<T\]/^^^^^^^^^ $$TO+;fnSRU>V>V+VWWWWWWWWW-$$$$$$$$$ -.........  %4x F F/FFFFGGGM 	  	  	  Q L 0Q00111	 s   D4D9 9
E3%E.(E3.E3c                   K   |dz  }t          t          |                    D ]c}|j                            d|dk    rdnd           d{V  t	          j        ddddt          d||z
  |d	z  z             z   z  z  z              ddS )
z
        Calculate a human-like scroll distance with overshoot.

        Args:
            distance (int): Intended scroll distance

        Returns:
            int: Adjusted scroll distance with overshoot
           r      Ng{Gz?g{Gz?g	@
   )rangeabsr    wheeltimer   pow)r
   r   r   hdds        r   r   zScrollPage._human_scrollR   s       ]s8}}%% 	Z 	ZA*""18a<<aaR@@@@@@@@@ JttqAGB2PR7?S=T0U0U,U'VWXYYYY		Z 	Zr   N)r   r   )__name__
__module____qualname____doc__r   r   intr   r$   r   r)   r    r   r   r   r      s         - - --  -  -  - S - TXY\^aYaTb -  -  -  - ^Z Z Z Z Z Z Z Z Zr   r   )
r9   r   r   r2   typingr   r   patchright.async_apir   r   r;   r   r   <module>r>      s                % % % % % %SZ SZ SZ SZ SZ SZ SZ SZ SZ SZr   