o
    B_i                      @  s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 ddlmZ ddlmZmZ ed	d
G dd dZd#ddZd$ddZd%ddZd&ddZd'd d!Zd"S )(    )annotations)	dataclass)Path)OptionalTuple)Page   )shot)log_steppauseT)frozenc                   @  s4   e Zd ZU ded< ded< ded< edd	d
ZdS )
TargetDateintyearmonthdayiso_datestrreturn'TargetDate'c                 C  s.   |   d\}}}tt|t|t|dS )z(
        iso_date: 'YYYY-MM-DD'
        -)r   r   r   )stripsplitr   r   )r   ymd r   a/var/www/html/flask_server/apps/aroflo_connector_app/ui_automation/flows/timesheet_select_date.pyparse   s   zTargetDate.parseN)r   r   r   r   )__name__
__module____qualname____annotations__staticmethodr   r   r   r   r   r      s   
 r   pager   r   Nonec                 C  s   | j ddd d S )Na  () => {
            const u = location.href.toLowerCase();
            if (!u.includes('timesheet')) return false;
            const t = document.body && (document.body.innerText || '');
            return t.includes('Daily Timesheet') || t.includes('Timesheet for');
        }iȯ  )timeoutwait_for_function)r$   r   r   r   _wait_timesheet_ready   s   
r)   Optional[str]c                 C  sH   z|  dj}| dkrW dS |d}|pdW S  ty#   Y dS w )uk   
    Usa el botón GPS que trae data-current-date="YYYY/MM/DD".
    Ej: data-current-date="2026/01/07"
    z*button.gpsWindowFullBtn[data-current-date]r   Nzdata-current-date)locatorfirstcountget_attribute	Exception)r$   btnvr   r   r   _get_current_date_from_dom)   s   

r2   dom_dater   c                 C  s>   |   d}t|dkr| S |d  d|d  d|d  S )z&
    'YYYY/MM/DD' -> 'YYYY-MM-DD'
    /   r   r      r   )r   r   len)r3   partsr   r   r   _normalize_dom_date_to_iso8   s    r9   
target_isoc                 C  s   | j d|dd dS )u   
    Espera que el DOM refleje la fecha objetivo (si está disponible).
    Si no está, al menos espera navegación/settle.
    aT  (targetIso) => {
            const btn = document.querySelector("button.gpsWindowFullBtn[data-current-date]");
            if (!btn) return false;
            const v = btn.getAttribute("data-current-date") || "";
            // v es YYYY/MM/DD
            const iso = v.replaceAll("/", "-");
            return iso === targetIso;
        }i N  )argr&   Nr'   )r$   r:   r   r   r   _wait_date_changedB   s
   
r<   run_dirr   target_datec          
      C  s|  t |  t|}|jdd|jdd|jd}t| }|r1t|}||kr1td| |  dS t	| |d|  td| |  t
|d|  d|j d	|j d
|j }| dj| d| dd}	|	 dkry| d| dj}	n|	j}	|	 dkrt	| |d|  td| d| d|	  t| | t |  t	| |d|  td| |  t
|d|  dS )z
    Cambia la fecha del Timesheet usando el calendario de AroFlo (a.calDay),
    seleccionando el link cuyo href contiene:
      sday=<D>&smonth=<M>&syear=<Y>

    target_date: 'YYYY-MM-DD' (ej '2026-01-08')
    04dr   02dzts-date-already-Nzts-date-01-before-zBefore selecting date zsday=z&smonth=z&syear=z.af-datepicker a.calDayza.calDay[href*='z'])hasr   z.af-datepicker a.calDay[href*='zts-date-99-not-found-u&   No encontré link del calendario para z	 (href*='z').zts-date-02-after-zAfter selecting date )r)   r   r   r   r   r   r2   r9   r
   r	   r   r+   filterr-   r,   RuntimeErrorclickr<   )
r$   cfgr=   r>   tgtr:   r3   cur_isohref_fragmentcal_linkr   r   r   runV   s8   
 

rJ   N)r$   r   r   r%   )r$   r   r   r*   )r3   r   r   r   )r$   r   r:   r   r   r%   )r$   r   r=   r   r>   r   r   r%   )
__future__r   dataclassesr   pathlibr   typingr   r   playwright.sync_apir   core.artifactsr	   core.logr
   r   r   r)   r2   r9   r<   rJ   r   r   r   r   <module>   s   




