
    F˝i                    |   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ d dlmZmZmZmZ 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  d dl!m"Z" 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-m.Z. erd dl/m0Z0  G d d      Z1 G d d      Z2 G d de      Z3 G d d      Z4 ed       G d  d!             Z5e G d" d#             Z6d5d$Z7 G d% d&      Z8 G d' d(      Z9 G d) d*      Z: G d+ d,      Z; G d- d.      Z<e G d/ d0             Z= G d1 d2      Z> G d3 d4      Z? e?       Z@y)6    )annotationsN)defaultdict)Mapping)	dataclassreplace)Enumauto)TYPE_CHECKINGAnyClassVaroverload)Self)loggers)
Dispatcher)CallableObjectCallbackType)extract_flags_from_object)Router)SceneException)StateFilter)
FSMContext)State)MemoryStorageRecord)TelegramObjectUpdate)ClassAttrsResolverget_sorted_mro_attrs_resolver)NextMiddlewareTypec                  V    e Zd ZdddZddZddZddZddZddZddZ	ddZ
dd	Zy
)HistoryManagerc                    || _         || _        t        |j                  t	        |j
                  |            | _        y )N)destiny)storagekey)_size_stater   r#   r   r$   _history_state)selfstater"   sizes       J/home/leshdev/mybot/venv/lib/python3.12/site-packages/aiogram/fsm/scene.py__init__zHistoryManager.__init__!   s2    
(MM		73
    c                ~  K   | j                   j                          d {   }|j                  dg       }|j                  ||d       t	        |      | j
                  kD  r|| j
                   d  }t        j                  j                  d||       | j                   j                  |       d {    y 7 7 w)Nhistoryr)   dataz Push state=%s data=%s to historyr/   )
r'   get_data
setdefaultappendlenr%   r   scenedebugupdate_data)r(   r)   r1   history_datar/   s        r+   pushzHistoryManager.push)   s     !0099;;)))R856w<$**$tzzkm,G>tL!!--g->>> < 	?s"   B=B9BB=3B;4B=;B=c                  K   | j                   j                          d {   }|j                  dg       }|sy |j                         }|d   }|d   }|s$| j                   j	                  i        d {    n$| j                   j                  |       d {    t        j                  j                  d||       t        ||      S 7 7 Y7 6w)Nr/   r)   r1   r2   z!Pop state=%s data=%s from historyr0   )
r'   r3   r4   popset_datar9   r   r7   r8   r   )r(   r:   r/   recordr)   r1   s         r+   r=   zHistoryManager.pop3   s     !0099;;)))R8wf~%%..r222%%11'1BBB?M"T:: < 3Bs4   CCAC4C5$CC3CCCc                   K   | j                   j                          d {   }|j                  dg       }|sy t        di |d   S 7 'w)Nr/    r'   r3   r4   r   )r(   r:   r/   s      r+   getzHistoryManager.getB   sL     !0099;;)))R8"1WR[11	 <s   A
A(A
c                   K   | j                   j                          d {   }|j                  dg       }|D cg c]  }t        di | c}S 7 /c c}w w)Nr/   rB   rC   )r(   r:   r/   items       r+   allzHistoryManager.allI   sQ     !0099;;)))R88?@#+d+@@ <@s    AAAAAAc                   K   t         j                  j                  d       | j                  j	                  i        d {    y 7 w)NzClear history)r   r7   r8   r'   r>   r(   s    r+   clearzHistoryManager.clearN   s2     O,!!**2...s   >A AAc                   K   | j                   j                          d {   }| j                   j                          d {   }| j                  ||       d {    y 7 A7 !7 	wN)r&   	get_stater3   r;   r(   r)   r1   s      r+   snapshotzHistoryManager.snapshotR   sQ     kk++--[[))++iit$$$ .+$s3   A(A"!A(A$A(A&A($A(&A(c                   K   | j                   j                  |       d {    | j                   j                  |       d {    y 7 (7 wrL   )r&   	set_stater>   rN   s      r+   
_set_statezHistoryManager._set_stateW   s?     kk##E***kk""4((( 	+(s!   AA
"AAAAc                ^  K   | j                          d {   }|s| j                  d i        d {    y t        j                  j	                  d|j
                  |j                         | j                  |j
                  |j                         d {    |j
                  S 7 7 v7 w)NzRollback to state=%s data=%s)r=   rR   r   r7   r8   r)   r1   )r(   previous_states     r+   rollbackzHistoryManager.rollback[   s     #xxz)//$+++*  	

 oon22N4G4GHHH### *+ 	Is2   B-B'B-B)A#B-B+B-)B-+B-N)scenes_history
   )r)   r   r"   strr*   int)r)   
str | Noner1   dict[str, Any]returnNone)r\   zMemoryStorageRecord | None)r\   zlist[MemoryStorageRecord]r\   r]   )r\   rZ   )__name__
__module____qualname__r,   r;   r=   rD   rG   rJ   rO   rR   rU   rB   r-   r+   r    r        s0    
?;2A
/%
)$r-   r    c                  d    e Zd Z	 	 d
	 	 	 	 	 	 	 	 	 ddZddZddZddZddZddZddZ	dd	Z
y)ObserverDecoratorNc                <    || _         || _        || _        || _        y rL   )namefiltersactionafter)r(   re   rf   rg   rh   s        r+   r,   zObserverDecorator.__init__k   s      	
r-   c                    t        |dd       }|s	g }||_        |j                  t        | j                  || j
                  | j                               y )N__aiogram_handler__)re   handlerrf   rh   )getattrrj   r5   HandlerContainerre   rf   rh   )r(   targethandlerss      r+   _wrap_filterzObserverDecorator._wrap_filterw   sM    6#8$?H)1F&YYjj		
r-   c                    | j                   J d       t        |dd       }|t        t              }||_        t        |      || j                      | j                  <   y )NzScene action is not specified__aiogram_action__)rg   rl   r   dictrr   r   re   )r(   rn   rg   s      r+   _wrap_actionzObserverDecorator._wrap_action   sZ    {{&G(GG&!5t<> &F(.F%)7)?t{{DII&r-   c                    t        j                  |      r2| j                  | j                  |       |S | j	                  |       |S d}t        |      )Nz"Only function or method is allowed)inspect
isfunctionrg   rp   rt   	TypeError)r(   rn   msgs      r+   __call__zObserverDecorator.__call__   sW    f%{{"!!&) 	 !!&)  7CC. r-   c                `    t        | j                  | j                  t        j                        S rL   )ActionContainerre   rf   SceneActionleaverI   s    r+   r~   zObserverDecorator.leave   s    tyy$,,8I8IJJr-   c                b    t        | j                  | j                  t        j                  |      S rL   )r|   re   rf   r}   enter)r(   rn   s     r+   r   zObserverDecorator.enter   s!    tyy$,,8I8I6RRr-   c                `    t        | j                  | j                  t        j                        S rL   )r|   re   rf   r}   exitrI   s    r+   r   zObserverDecorator.exit       tyy$,,8H8HIIr-   c                `    t        | j                  | j                  t        j                        S rL   )r|   re   rf   r}   backrI   s    r+   r   zObserverDecorator.back   r   r-   )NN)
re   rX   rf   tuple[CallbackType, ...]rg   zSceneAction | Nonerh   After | Noner\   r]   )rn   ztype[Scene] | CallbackTyper\   r]   )rn   r   r\   r]   )rn   r   r\   r   )r\   r|   )rn   type[Scene]r\   r|   )r_   r`   ra   r,   rp   rt   rz   r~   r   r   r   rB   r-   r+   rc   rc   j   sg    
 &*"

 *
 #	

 
 


@	KSJJr-   rc   c                  D    e Zd Z e       Z e       Z e       Z e       Zy)r}   N)r_   r`   ra   r	   r   r~   r   r   rB   r-   r+   r}   r}      s    FEFE6D6Dr-   r}   c                  2    e Zd Z	 d	 	 	 	 	 	 	 	 	 ddZddZy)r|   Nc                <    || _         || _        || _        || _        y rL   )re   rf   rg   rn   )r(   re   rf   rg   rn   s        r+   r,   zActionContainer.__init__   s      	r-   c                  K   | j                   t        j                  k(  r0| j                  $|j	                  | j                         d {    y | j                   t        j
                  k(  r|j                          d {    y | j                   t        j                  k(  r|j                          d {    y | j                   t        j                  k(  r|j                          d {    y y 7 7 t7 @7 wrL   )rg   r}   r   rn   gotor~   r   r   r(   wizards     r+   executezActionContainer.execute   s     ;;++++0G++dkk***[[K---,,.  [[K,,,++-[[K,,,++- - + sH   AC;
C35C; C55C;6C775C;,C9-C;5C;7C;9C;rL   )
re   rX   rf   r   rg   r}   rn    type[Scene] | State | str | Noner\   r]   r   SceneWizardr\   r]   )r_   r`   ra   r,   r   rB   r-   r+   r|   r|      sA     48

 *
 	

 1
 

 r-   r|   T)slotsc                  :    e Zd ZU ded<   ded<   ded<   dZded	<   y)
rm   rX   re   r   rk   r   rf   Nr   rh   )r_   r`   ra   __annotations__rh   rB   r-   r+   rm   rm      s    
I%%E<r-   rm   c                  p    e Zd ZU ded<   	 ded<   	 ded<   	 dZded	<   	 dZded
<   	 dZded<   	 eZded<   y)SceneConfigrZ   r)   zlist[HandlerContainer]ro   z,dict[SceneAction, dict[str, CallableObject]]actionsNzbool | Nonereset_data_on_enterreset_history_on_entercallback_query_without_stater   attrs_resolver)	r_   r`   ra   r   r   r   r   r   r   rB   r-   r+   r   r      sW    $$99'++#*.K.&04 +4,)FN&Fr-   r   c                    K   y wrL   rB   )argskwargss     r+   _empty_handlerr      s	        c                  J    e Zd Z	 d	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd	dZd
dZy)SceneHandlerWrapperNc                @    || _         t        |      | _        || _        y rL   )r7   r   rk   rh   )r(   r7   rk   rh   s       r+   r,   zSceneHandlerWrapper.__init__   s     
%g.
r-   c           
     .  K   	 |d   }|d   }|d   }| j                  t	        | j                  j
                  |||j                  ||            }	 | j                  j                  |	|fi | d {   }
| j                  rYt        d	d
| j                  j                  | j                  j                        }|j                  |	j                         d {    |
S # t         $ r&}|j                  d   }d|d}t        |      d d }~ww xY w7 7 :w)Nr)   scenesr   zScene context key z@ is not available. Ensure FSM is enabled and pipeline is intact.event_updatescene_configmanagerr)   update_typeeventr1   r   rh   rB   )KeyErrorr   r   r7   r   __scene_config__
event_typerk   callrh   r|   rg   r   r   )r(   r   r   r)   r   errormissing_keyry   r   r7   resultaction_containers               r+   rz   zSceneHandlerWrapper.__call__   s,    
		0 &wE$*8$4F  &n5

!ZZ88(33  	
 )t||((@@@::.

!!

  	  #**5<<888;  	0**Q-K$[O 4@ @  !%4/	0& A 9sG   D
C A%D3D4A$DDD	D(!D		DDDc                    | S rL   rB   rI   s    r+   	__await__zSceneHandlerWrapper.__await__  s    r-   c                    d| j                    d| j                  j                   }| j                  r|d| j                   z  }|dz  }|S )NzSceneHandlerWrapper(z, z, after=))r7   rk   callbackrh   )r(   r   s     r+   __str__zSceneHandlerWrapper.__str__   sM    '

|2dll6K6K5LM::--F#r-   rL   )r7   r   rk   r   rh   r   r\   r]   )r   r   r   r   r\   r   )r\   r   )r\   rX   )r_   r`   ra   r,   rz   r   r   rB   r-   r+   r   r      s^    
 #	  	
 
%% % 
	%Nr-   r   c                  z     e Zd ZU dZded<   	 	 	 	 	 d	dZd
 fdZedd       Zeddd       Z	edd       Z
 xZS )Scenea  
    Represents a scene in a conversation flow.

    A scene is a specific state in a conversation where certain actions can take place.

    Each scene has a set of filters that determine when it should be triggered,
    and a set of handlers that define the actions to be executed when the scene is active.

    .. note::
        This class is not meant to be used directly. Instead, it should be subclassed
        to define custom scenes.
    zClassVar[SceneConfig]r   c                4    || _         | | j                   _        y rL   )r   r7   r   s     r+   r,   zScene.__init__9  s      r-   c           	        |j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }t        |   di | g }t        t              }| j
                  D ]\  }	t        |	t              st        |	dd       }
|
s$||
j                  }||
j                  }||
j                  }|Q|
j                  }^ |t        } ||       D ]  \  }}t        |dd       x}r|j                  |       t        |t               r@|j#                  t%        |j&                  t(        |j*                  |j,                               t/        |d	      s|j0                  j3                         D ]  \  }}||   j5                  |         t7        ||t	        |      ||||
      | _        y )Nr)   r   r   r   r   r   rj   rh   rr   )r)   ro   r   r   r   r   r   rB   )r=   super__init_subclass__r   rs   	__bases__
issubclassr   rl   r   r   r   r   r   extend
isinstancerc   r5   rm   re   r   rf   rh   hasattrrr   itemsupdater   r   )clsr   
state_namer   r   r   r   ro   r   baseparent_scene_config_namevaluescene_handlersrg   action_handlers	__class__s                   r+   r   zScene.__init_subclass__@  s   ZZ.
$jj)>E!',Dd!K'-zz2PRV'W$$4d;!+F++-GRSWGXMM 	DDdE*")$0BD"I&"*&9&M&M#%-)<)S)S&+3/B/_/_,%!4!C!C	D" !:N*3/ 	<LE5!(0Et!LL~L/%!23$

&#kk	 u23/4/G/G/M/M/O <+FOFO**?;<	<   +M 3#9)E) 
r-   c                   | j                   }t               }|j                  D ]  } |j                  |j                     j
                  t        | |j                  |j                        g|j                  dt        |j                        i |j                  |j                          |D ]E  }|j                  r|dk(  r|j                  |   j                  t        |j                               G y)z^
        Adds the scene to the given router.

        :param router:
        :return:
        r   flagscallback_queryN)r   setro   	observersre   registerr   rk   rh   rf   r   addr   filterr   r)   )r   routerr   used_observersrk   observer_names         r+   add_to_routerzScene.add_to_routerz  s     ++#,, 
	-G3FW\\*33#OO!--  0@ w||,
	- , 	TM88]N^=^]+22;|?Q?Q3RS	Tr-   c                    |3d| j                    d| j                   d| j                  j                  }t	        |      }| j                  |       |S )zM
        Returns the scene as a router.

        :return: new router
        zScene '.z' for state re   )r`   ra   r   r)   r   r   )r   re   r   s      r+   	as_routerzScene.as_router  sb     <#..)3+;+;*< = 1177:<  T"&!r-   c                (     	 	 	 	 	 	 	 	 d fd}|S )z
        Create an entry point handler for the scene, can be used to simplify the handler
        that starts the scene.

        >>> router.message.register(MyScene.as_handler(), Command("start"))
        c                P   K    |j                   fi i | d {    y 7 wrL   )r   )r   r   middleware_kwargsr   handler_kwargss      r+   enter_to_scene_handlerz0Scene.as_handler.<locals>.enter_to_scene_handler  s.     
 &,,sN&M&M;L&MNNNs   &$&)r   r   r   ScenesManagerr   r   r\   r]   rB   )r   r   r   s   `` r+   
as_handlerzScene.as_handler  s9    	O!	O!	O "%	O 		O &%r-   r   r   r   r\   r]   r   r   r\   r]   rL   )re   rZ   r\   r   )r   r   r\   r   )r_   r`   ra   __doc__r   r,   r   classmethodr   r   r   __classcell__)r   s   @r+   r   r   (  sn     ,+!! 
!8
t T T6   & &r-   r   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdddZddZddZddZ	ddZ
dd	Zdd
ZddZedd       Zedd       ZdddZ	 d	 	 	 	 	 ddZddZy) r   as  
    A class that represents a wizard for managing scenes in a Telegram bot.

    Instance of this class is passed to each scene as a parameter.
    So, you can use it to transition between scenes, get and set data, etc.

    .. note::

        This class is not meant to be used directly. Instead, it should be used
        as a parameter in the scene constructor.

    c                f    || _         || _        || _        || _        || _        || _        d| _        y)a  
        A class that represents a wizard for managing scenes in a Telegram bot.

        :param scene_config: The configuration of the scene.
        :param manager: The scene manager.
        :param state: The FSMContext object for storing the state of the scene.
        :param update_type: The type of the update event.
        :param event: The TelegramObject represents the event.
        :param data: Additional data for the scene.
        N)r   r   r)   r   r   r1   r7   )r(   r   r   r)   r   r   r1   s          r+   r,   zSceneWizard.__init__  s7    & )
&
	#'
r-   c                :  K   t         j                  j                  d| j                  j                         | j                  j
                  r#| j                  j                  i        d{    | j                  j                  r,| j                  j                  j                          d{    | j                  j                  | j                  j                         d{     | j                  t        j                  fi | d{    y7 7 g7 27 w)a  
        Enter method is used to transition into a scene in the SceneWizard class.
        It sets the state, clears data and history if specified,
        and triggers entering event of the scene.

        :param kwargs: Additional keyword arguments.
        :return: None
        zEntering scene %rN)r   r7   r8   r   r)   r   r>   r   r   r/   rJ   rQ   
_on_actionr}   r   r(   r   s     r+   r   zSceneWizard.enter  s      	/1B1B1H1HI00**%%b)))33,,&&,,...jj""4#4#4#:#:;;;dook//:6:::	 *.;:sI   A)D+D,AD-D.6D$D%(DDDDDDc                *  K   t         j                  j                  d| j                  j                         |r,| j
                  j                  j                          d{     | j                  t        j                  fi | d{    y7 .7 w)a"  
        Leaves the current scene.
        This method is used to exit a scene and transition to the next scene.

        :param _with_history: Whether to include history in the snapshot. Defaults to True.
        :param kwargs: Additional keyword arguments.
        :return: None

        zLeaving scene %rN)r   r7   r8   r   r)   r   r/   rO   r   r}   r~   )r(   _with_historyr   s      r+   r~   zSceneWizard.leave  sp      	.0A0A0G0GH,,&&//111dook//:6::: 2:s$   AB B!(B	B
BBc                v  K   t         j                  j                  d| j                  j                         | j
                  j                  j                          d{     | j                  t        j                  fi | d{     | j
                  j                  dddi| d{    y7 T7 -7 	w)z
        Exit the current scene and enter the default scene/state.

        :param kwargs: Additional keyword arguments.
        :return: None
        zExiting scene %rN_check_activeFrL   )r   r7   r8   r   r)   r   r/   rJ   r   r}   r   r   r   s     r+   r   zSceneWizard.exit  s      	.0A0A0G0GHll""((***dook..9&999 dll  EUEfEEE 	+9Es6   AB9B3(B9B5%B9-B7.B95B97B9c                ^  K   t         j                  j                  d| j                  j                          | j
                  dddi| d{    | j                  j                  j                          d{   } | j                  j                  |fddi| d{    y7 X7 .7 	w)z
        This method is used to go back to the previous scene.

        :param kwargs: Keyword arguments that can be passed to the method.
        :return: None
        z$Back to previous scene from scene %sr   FNr   rB   )
r   r7   r8   r   r)   r~   r   r/   rU   r   )r(   r   	new_scenes      r+   r   zSceneWizard.back  s      	BDDUDUD[D[\djj7u7777,,..7799	 dll  J%J6JJJ 	89Js6   AB-B'+B-:B);&B-!B+"B-)B-+B-c                   K   | j                   j                  J d        | j                  | j                   j                  fi | d{    y7 w)z
        This method allows to re-enter the current scene.

        :param kwargs: Additional keyword arguments to pass to the scene.
        :return: None
        NzScene state is not specified)r   r)   r   r   s     r+   retakezSceneWizard.retake  sK        &&2R4RR2dii))//:6:::s   AA
AAc                   K    | j                   di | d{     | j                  j                  |fddi| d{    y7 ,7 w)a  
        The `goto` method transitions to a new scene.
        It first calls the `leave` method to perform any necessary cleanup
        in the current scene, then calls the `enter` event to enter the specified scene.

        :param scene: The scene to transition to. Can be either a `Scene` instance
            `State` instance or a string representing the scene.
        :param kwargs: Additional keyword arguments to pass to the `enter`
            method of the scene manager.
        :return: None
        Nr   FrB   )r~   r   r   )r(   r7   r   s      r+   r   zSceneWizard.goto%  sL      djj"6""" dll  FeFvFFF 	#Fs    A	A&A	A A	A	c                  K   | j                   sd}t        |      t        j                   j                  d|j                  | j
                  j                         | j
                  j                  j                  |i       }|s@t        j                   j                  d|j                  | j
                  j                         y| j                  }||vrAt        j                   j                  d|j                  || j
                  j                         y ||   j                  | j                   | j                  fi i | j                  | d {    y7 w)NzScene is not initializedzCall action %r in scene %rzAction %r not found in scene %rFz,Action %r for event %r not found in scene %rT)r7   r   r   r8   re   r   r)   r   rD   r   r   r   r1   )r(   rg   r   ry   action_configr   s         r+   r   zSceneWizard._on_action4  s    zz,C %%8&++tGXGXG^G^_))1155fbAMM1!!''
 %%
]*MM>!!''	 ,mJ',,TZZ_G^$))G^W]G^___ 	`s   EEEEc                X   K   | j                   j                  |       d{    y7 w)z
        Sets custom data in the current state.

        :param data: A mapping containing the custom data to be set in the current state.
        :return: None
        r1   N)r)   r>   )r(   r1   s     r+   r>   zSceneWizard.set_dataP  s#      jj!!t!,,,s    *(*c                R   K   | j                   j                          d{   S 7 w)z
        This method returns the data stored in the current state.

        :return: A dictionary containing the data stored in the scene state.
        N)r)   r3   rI   s    r+   r3   zSceneWizard.get_dataY  s!      ZZ((****s   '%'c                   K   yw)z
        This method returns the value from key in the data of the current state.

        :param key: The keyname of the item you want to return the value from.

        :return: A dictionary containing the data stored in the scene state.
        NrB   )r(   r$   s     r+   	get_valuezSceneWizard.get_valuea       r   c                   K   yw)aC  
        This method returns the value from key in the data of the current state.

        :param key: The keyname of the item you want to return the value from.
        :param default: Default value to return, if ``key`` was not found.

        :return: A dictionary containing the data stored in the scene state.
        NrB   r(   r$   defaults      r+   r  zSceneWizard.get_valuek  r  r   Nc                V   K   | j                   j                  ||       d {   S 7 wrL   )r)   r  r  s      r+   r  zSceneWizard.get_valuev  s#     ZZ))#w7777s    )')c                |   K   |r|j                  |       | j                  j                  |       d{   S 7 w)z
        This method updates the data stored in the current state

        :param data: Optional mapping of data to update.
        :param kwargs: Additional key-value pairs of data to update.
        :return: Dictionary of updated data
        r  N)r   r)   r9   )r(   r1   r   s      r+   r9   zSceneWizard.update_datay  s5      MM$ZZ+++8888s   3<:<c                B   K   | j                  i        d{    y7 w)z9
        Clears the data.

        :return: None
        N)r>   rI   s    r+   
clear_datazSceneWizard.clear_data  s      mmBs   )r   r   r   r   r)   r   r   rX   r   r   r1   r[   r   T)r   boolr   r   r\   r]   )r7   ztype[Scene] | State | strr   r   r\   r]   )rg   r}   r   r   r\   r  )r1   zMapping[str, Any]r\   r]   )r\   r[   )r$   rX   r\   
Any | None)r$   rX   r  r   r\   r   rL   )r$   rX   r  r  r\   r  )r1   zMapping[str, Any] | Noner   r   r\   r[   r^   )r_   r`   ra   r   r,   r   r~   r   r   r   r   r   r>   r3   r   r  r9   r  rB   r-   r+   r   r     s    (!( ( 	(
 ( ( (8;";
F
K;G8-+    8
 *.9&9 9 
	9  r-   r   c                  b    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 ddZd	dZd
dZ	 d	 	 	 	 	 	 	 ddZddZy)r   z
    The ScenesManager class is responsible for managing scenes in an application.
    It provides methods for entering and exiting scenes, as well as retrieving the active scene.
    c                ~    || _         || _        || _        || _        || _        t        | j                        | _        y rL   )registryr   r   r)   r1   r    r/   )r(   r  r   r   r)   r1   s         r+   r,   zScenesManager.__init__  s8     !&

	%djj1r-   c           
        K   | j                   j                  |      } |t        |j                  | | j                  | j
                  | j                  | j                              S w)Nr   r   )r  rD   r   r   r)   r   r   r1   )r(   
scene_types     r+   
_get_scenezScenesManager._get_scene  sY     ]]&&z2
'88jj ,,jjYY	
 		
s   A&A(c                   K   | j                   j                          d {   }	 | j                  |       d {   S 7 7 # t        $ r Y y w xY wwrL   )r)   rM   r  r   )r(   r)   s     r+   _get_active_scenezScenesManager._get_active_scene  sL     jj**,,	/// -/ 		s@   A?AA AA AA 	AAAAc                  K   |r>| j                          d{   }|$ |j                  j                  di | d{    	 | j                  |       d{   } |j                  j                  di | d{    y7 i7 E7 -7 # t
        $ r* | | j                  j                  d       d{  7   Y yw xY ww)a  
        Enters the specified scene.

        :param scene_type: Optional Type[Scene], State or str representing the scene type to enter.
        :param _check_active: Optional bool indicating whether to check if
            there is an active scene to exit before entering the new scene. Defaults to True.
        :param kwargs: Additional keyword arguments to pass to the scene's wizard.enter() method.
        :return: None
        NrB   )r  r   r   r  r   r   r)   rQ   )r(   r  r   r   active_scener7   s         r+   r   zScenesManager.enter  s      !%!7!7!99L'.l))..8888	///*55E %%,,$$.v... :8 6 /  	-%**&&t,,,	-ss   C B%C BC B
 BB
 C <B=C C B
 C 
*B=4B75B=:C <B==C c                   K   | j                          d{   }|sy |j                  j                  di | d{    y7 ,7 w)z
        Close method is used to exit the currently active scene in the ScenesManager.

        :param kwargs: Additional keyword arguments passed to the scene's exit method.
        :return: None
        NrB   )r  r   r   )r(   r   r7   s      r+   closezScenesManager.close  sG      ,,..ell)&))) / 	*s   AA&AAAAN)r  SceneRegistryr   rX   r   r   r)   r   r1   r[   r\   r]   )r  r   r\   r   )r\   zScene | Noner  )r  r   r   r  r   r   r\   r]   r   )	r_   r`   ra   r   r,   r  r  r   r  rB   r-   r+   r   r     s    
22 2 	2
 2 2 
2 
 #/4/ / 	/
 
/:
*r-   r   c                  p    e Zd ZdZdddZddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZddddZdd	Z	dd
Z
y)r  zJ
    A class that represents a registry for scenes in a Telegram bot.
    c                P    || _         || _        i | _        | j                  |       y)z
        Initialize a new instance of the SceneRegistry class.

        :param router: The router instance used for scene registration.
        :param register_on_add: Whether to register the scenes to the router when they are added.
        N)r   register_on_add_scenes_setup_middleware)r(   r   r  s      r+   r,   zSceneRegistry.__init__  s(     .68v&r-   c                   t        |t              r&|j                  j                  | j                         y |j
                  j                         D ],  }|j                  dv r|j                  | j                         . y )N>   r   r   )	r   r   r   outer_middleware_update_middlewarer   values
event_name_middleware)r(   r   observers      r+   r!  zSceneRegistry._setup_middleware  sm    fj) MM**4+B+BC((//1 	8H""&99%%d&6&67	8r-   c                   K   t        |t              sJ d       |j                  d      }| |||       d {   S t        | |j                  |j
                  ||      |d<    |||       d {   S 7 <7 w)Nz Event must be an Update instancer)   r  r   r   r)   r1   r   )r   r   rD   r   r   r   )r(   rk   r   r1   r)   s        r+   r$  z SceneRegistry._update_middleware  s      %(L*LL(!= ---&((++
X UD))) . *s!   7A:A67A:1A82A:8A:c                   K   |j                  d      }| |||       d {   S |d   }t        | |j                  |||      |d<    |||       d {   S 7 77 w)Nr)   r   r*  r   )rD   r   r   )r(   rk   r   r1   r)   r   s         r+   r'  zSceneRegistry._middleware  su      != ---n-&))
X UD))) . *s!    AA2AAAAN)r   c                  |sd}t        |      |D ]  }|j                  j                  | j                  v r%d|j                  j                  d}t	        |      || j                  |j                  j                  <   |r |j                  |j                                | j                  s| j                  j                  |j                                 y)a  
        This method adds the specified scenes to the registry
        and optionally registers it to the router.

        If a scene with the same state already exists in the registry, a SceneException is raised.

        .. warning::

            If the router is not specified, the scenes will not be registered to the router.
            You will need to include the scenes manually to the router or use the register method.

        :param scenes: A variable length parameter that accepts one or more types of scenes.
            These scenes are instances of the Scene class.
        :param router: An optional parameter that specifies the router
            to which the scenes should be added.
        :return: None
        z$At least one scene must be specifiedzScene with state z already existsN)	
ValueErrorr   r)   r   r   include_routerr   r  r   )r(   r   r   ry   r7   s        r+   r   zSceneRegistry.add,  s    $ 8CS/! 
	>E%%++t||;)%*@*@*F*F)IY$S))9>DLL//556%%eoo&78%%**5??+<=
	>r-   c                <     | j                   |d| j                  i y)z
        Registers one or more scenes to the SceneRegistry.

        :param scenes: One or more scene classes to register.
        :return: None
        r   N)r   r   )r(   r   s     r+   r   zSceneRegistry.registerN  s     	&--r-   c                L   t        j                  |      r&t        |t              r|j                  j
                  }t        |t              r|j
                  }|t        |t              sd}t        |      	 | j                  |   S # t        $ r d|d}t        |      dw xY w)ae  
        This method returns the registered Scene object for the specified scene.
        The scene parameter can be either a Scene object, State object or a string representing
        the name of the scene. If a Scene object is provided, the state attribute
        of the SceneConfig object associated with the Scene object will be used as the scene name.
        If a State object is provided, the state attribute of the State object will be used as the
        scene name. If None or an invalid type is provided, a SceneException will be raised.

        If the specified scene is not registered in the SceneRegistry object,
        a SceneException will be raised.

        :param scene: A Scene object, State object or a string representing the name of the scene.
        :return: The registered Scene object corresponding to the given scene parameter.

        Nz4Scene must be a subclass of Scene, State or a stringzScene z is not registered)rv   isclassr   r   r   r)   r   r   rX   r   r   r   )r(   r7   ry   s      r+   rD   zSceneRegistry.getW  s      ??5!j&>**00EeU#KKEZs%;HC %%	0<<&& 	05)#56C %4/	0s   8B B#r  )r   r   r  r  r\   r]   r   )rk   z"NextMiddlewareType[TelegramObject]r   r   r1   r[   r\   r   )r   r   r   zRouter | Noner\   r]   )r   r   r\   r]   )r7   r   r\   r   )r_   r`   ra   r   r,   r!  r$  r'  r   r   rD   rB   r-   r+   r  r    s~    '
8*3* * 	*
 
*(*3* * 	*
 
*( AE  >D.0r-   r  c                  \    e Zd ZU ded<   dZded<   ed	d       Zed	d       Zed
d       Zy)Afterr}   rg   Nr   r7   c                0     | t         j                        S Nrg   )r}   r   r   s    r+   r   z
After.exit{      +**++r-   c                0     | t         j                        S r5  )r}   r   r7  s    r+   r   z
After.back  r8  r-   c                2     | t         j                  |      S )N)rg   r7   )r}   r   )r   r7   s     r+   r   z
After.goto  s    +++599r-   )r\   r3  )r7   r   r\   r3  )	r_   r`   ra   r   r7   r   r   r   r   rB   r-   r+   r3  r3  v  sM    .2E+2, , , , : :r-   r3  c                  L    e Zd Zd	dZdd	 	 	 	 	 d
dZddZddZddZddZy)ObserverMarkerc                    || _         y rL   r   )r(   re   s     r+   r,   zObserverMarker.__init__  s	    	r-   Nr   c               2    t        | j                  ||      S )Nr   )rc   re   )r(   rh   rf   s      r+   rz   zObserverMarker.__call__  s    
 !II
 	
r-   c                N    t        | j                  |t        j                        S r5  )rc   re   r}   r   )r(   rf   s     r+   r   zObserverMarker.enter  s     GK<M<MNNr-   c                N    t        | j                  dt        j                        S NrB   r6  )rc   re   r}   r~   rI   s    r+   r~   zObserverMarker.leave  s     B{7H7HIIr-   c                N    t        | j                  dt        j                        S rA  )rc   re   r}   r   rI   s    r+   r   zObserverMarker.exit       B{7G7GHHr-   c                N    t        | j                  dt        j                        S rA  )rc   re   r}   r   rI   s    r+   r   zObserverMarker.back  rC  r-   )re   rX   r\   r]   )rf   r   rh   r   r\   rc   )rf   r   r\   rc   )r\   rc   )	r_   r`   ra   r,   rz   r   r~   r   r   rB   r-   r+   r<  r<    sE     #	
	
 	
 
		
OJIIr-   r<  c                      e Zd ZdZ ed      Z ed      Z ed      Z ed      Z ed      Z	 ed      Z
 ed      Z ed	      Z ed
      Z ed      Z ed      Z ed      Z ed      Z ed      Zy)OnMarkera}  
    The `OnMarker` class is used as a marker class to define different
    types of events in the Scenes.

    Attributes:

    - :code:`message`: Event marker for handling `Message` events.
    - :code:`edited_message`: Event marker for handling edited `Message` events.
    - :code:`channel_post`: Event marker for handling channel `Post` events.
    - :code:`edited_channel_post`: Event marker for handling edited channel `Post` events.
    - :code:`inline_query`: Event marker for handling `InlineQuery` events.
    - :code:`chosen_inline_result`: Event marker for handling chosen `InlineResult` events.
    - :code:`callback_query`: Event marker for handling `CallbackQuery` events.
    - :code:`shipping_query`: Event marker for handling `ShippingQuery` events.
    - :code:`pre_checkout_query`: Event marker for handling `PreCheckoutQuery` events.
    - :code:`poll`: Event marker for handling `Poll` events.
    - :code:`poll_answer`: Event marker for handling `PollAnswer` events.
    - :code:`my_chat_member`: Event marker for handling my chat `Member` events.
    - :code:`chat_member`: Event marker for handling chat `Member` events.
    - :code:`chat_join_request`: Event marker for handling chat `JoinRequest` events.
    - :code:`error`: Event marker for handling `Error` events.

    .. note::

        This is a marker class and does not contain any methods or implementation logic.
    messageedited_messagechannel_postedited_channel_postinline_querychosen_inline_resultr   shipping_querypre_checkout_querypollpoll_answermy_chat_memberchat_memberchat_join_requestN)r_   r`   ra   r   r<  rG  rH  rI  rJ  rK  rL  r   rM  rN  rO  rP  rQ  rR  rS  rB   r-   r+   rF  rF    s    6 Y'G#$45N!.1L()>?!.1L)*@A#$45N#$45N'(<=&!D /K#$45N /K&':;r-   rF  )r   r   r   r   r\   r]   )A
__future__r   rv   collectionsr   collections.abcr   dataclassesr   r   enumr   r	   typingr
   r   r   r   typing_extensionsr   aiogramr   aiogram.dispatcher.dispatcherr    aiogram.dispatcher.event.handlerr   r   aiogram.dispatcher.flagsr   aiogram.dispatcher.routerr   aiogram.exceptionsr   aiogram.filtersr   aiogram.fsm.contextr   aiogram.fsm.stater   aiogram.fsm.storage.memoryr   aiogram.typesr   r   "aiogram.utils.class_attrs_resolverr   r   aiogram.dispatcher.event.basesr   r    rc   r}   r|   rm   r   r   r   r   r   r   r  r3  r<  rF  onrB   r-   r+   <module>ri     s2   "  # # *  9 9 "  4 I > , - ' * # : 0
 AG$ G$T:J :Jz$    0      0	: :zN& N&bV  V rQ* Q*hM0 M0` : : :"I I8)< )<X Zr-   