
    i9                        d 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Zn# e$ r dZY nw xY w G d	 d
ee          Z G d de          Zej        ddfddZej        ddfd dZd!dZd!dZd"dZd#dZdS )$a  Text collation support for string comparisons.

This module provides collation (text comparison) functionality with optional
PyICU support for advanced Unicode collation. Falls back to simple binary
and case-insensitive comparisons when PyICU is not available.
    )annotations)Callable)Enum)Collator)LocaleTFc                  "    e Zd ZdZdZ	 dZ	 dZdS )	Collationu  Text comparison collation strategies.

    For most users, use case_sensitive parameter in add_property_filter()
    instead of working with Collation directly.

    Examples:
        # Simple API (recommended for most users):
        searcher.add_property_filter("SUMMARY", "meeting", case_sensitive=False)

        # Advanced API (for power users):
        searcher.add_property_filter("SUMMARY", "Müller",
                                    collation=Collation.LOCALE,
                                    locale="de_DE")
    simpleunicodelocaleN)__name__
__module____qualname____doc__SIMPLEUNICODELOCALE     W/root/projects/butler/venv/lib/python3.11/site-packages/icalendar_searcher/collation.pyr	   r	      s=          F G' F< <r   r	   c                      e Zd ZdZdS )CollationErrorz4Raised when collation operation cannot be performed.N)r   r   r   r   r   r   r   r   r   ?   s        >>Dr   r   N	collationcase_sensitiveboolr   
str | NonereturnCallable[[str, str], bool]c                P   | t           j        k    r|rt          S t          S | t           j        t           j        fv r[t          st          d|  d          | t           j        k    r!|st          d          t          ||          S t          d|          S t          d|            )a  Get a collation function for substring matching.

    Args:
        collation: The collation strategy to use
        case_sensitive: Whether comparison should be case-sensitive
        locale: Locale string (e.g., "de_DE", "en_US") for LOCALE collation

    Returns:
        A function that takes (needle, haystack) and returns True if needle
        is found in haystack according to the collation rules.

    Raises:
        CollationError: If PyICU is required but not available, or if
                       invalid parameters are provided.

    Examples:
        >>> match_fn = get_collation_function(Collation.SIMPLE, case_sensitive=False)
        >>> match_fn("test", "This is a TEST")
        True
    Collation '[' requires PyICU to be installed. Install with: pip install 'icalendar-searcher[collation]',LOCALE collation requires a locale parameterNUnknown collation: )	r	   r   _binary_contains_case_insensitive_containsr   r   	HAS_PYICUr   _get_icu_containsr   r   r   s      r   get_collation_functionr)   E   s    2 I$$$ 	.##--	y()*:;	;	; 	 Li L L L  
 	((( U$%STTT$V^<<< %T>::: >9>>???r   Callable[[str], bytes]c                @   | t           j        k    r|rd S d S | t           j        t           j        fv r[t          st          d|  d          | t           j        k    r!|st          d          t          ||          S t          d|          S t          d|            )a  Get a collation function for generating sort keys.

    Args:
        collation: The collation strategy to use
        case_sensitive: Whether comparison should be case-sensitive
        locale: Locale string (e.g., "de_DE", "en_US") for LOCALE collation

    Returns:
        A function that takes a string and returns a sort key (bytes) that
        can be used for sorting according to the collation rules.

    Raises:
        CollationError: If PyICU is required but not available, or if
                       invalid parameters are provided.

    Examples:
        >>> sort_key_fn = get_sort_key_function(Collation.SIMPLE, case_sensitive=False)
        >>> sorted(["Zebra", "apple", "Banana"], key=sort_key_fn)
        ['apple', 'Banana', 'Zebra']
    c                ,    |                      d          S Nzutf-8)encodess    r   <lambda>z'get_sort_key_function.<locals>.<lambda>   s    QXXg.. r   c                P    |                                                      d          S r-   )lowerr.   r/   s    r   r1   z'get_sort_key_function.<locals>.<lambda>   s    QWWYY--g66 r   r    r!   r"   Nr#   )r	   r   r   r   r&   r   _get_icu_sort_keyr(   s      r   get_sort_key_functionr5   w   s    2 I$$$ 	7...666	y()*:;	;	; 	 Li L L L  
 	((( U$%STTT$V^<<< %T>::: >9>>???r   needlestrhaystackc                
    | |v S )z(Binary (case-sensitive) substring match.r   r6   r8   s     r   r$   r$      s    Xr   c                R    |                                  |                                 v S )z!Case-insensitive substring match.r3   r:   s     r   r%   r%      s    <<>>X^^----r   c                    dfd}|S )aG  Get ICU-based substring matcher.

    Note: This is a simplified implementation. PyICU doesn't expose ICU's
    StringSearch API which would be needed for proper substring matching with
    collation. For now, we use Python's built-in matching.

    Future enhancement: Implement proper collation-aware substring matching.
    r6   r7   r8   r   r   c                `    r| |v S |                                  |                                 v S )zCheck if needle is in haystack.

        This is a fallback implementation until proper ICU StringSearch support
        is added. It provides reasonable behavior for most use cases.
        r<   )r6   r8   r   s     r   icu_containsz'_get_icu_contains.<locals>.icu_contains   s5      	6X%%<<>>X^^%5%555r   r6   r7   r8   r7   r   r   r   )r   r   r?   s    ` r   r'   r'      s)    6 6 6 6 6 6 r   c                   | rt          |           nt          j                    }t          j        |          |r                     t          j                   n                    t          j                   dfd}|S )zGet ICU-based sort key function.

    Creates a collator instance and returns a function that generates sort keys.
    The collator strength is configured based on case_sensitive parameter.
    r0   r7   r   bytesc                .                         |           S )z Generate ICU collation sort key.)
getSortKey)r0   collators    r   icu_sort_keyz'_get_icu_sort_key.<locals>.icu_sort_key   s    ""1%%%r   )r0   r7   r   rB   )	ICULocalegetRootICUCollatorcreateInstancesetStrengthTERTIARY	SECONDARY)r   r   
icu_localerF   rE   s       @r   r4   r4      s     '-E6""")2C2E2EJ)*55H  4[12222[2333& & & & & & r   )r   r	   r   r   r   r   r   r   )r   r	   r   r   r   r   r   r*   r@   )r   r   r   r   r   r   )r   r   r   r   r   r*   )r   
__future__r   collections.abcr   enumr   icur   rI   r   rG   r&   ImportErrorr7   r	   	Exceptionr   r   r)   r5   r$   r%   r'   r4   r   r   r   <module>rU      s    # " " " " " $ $ $ $ $ $      ++++++''''''II   III%< %< %< %< %<T %< %< %<P	 	 	 	 	Y 	 	 	 %+/@ /@ /@ /@ /@f %+/@ /@ /@ /@ /@n   
. . . .
   4     s   % //