o
    .j9                     @  s   d Z ddlmZ ddlmZ ddlmZ zddlmZ	 ddlm
Z dZW n ey/   dZY nw G d	d
 d
eeZG dd deZejddfd%ddZejddfd&ddZd'ddZd'dd Zd(d!d"Zd)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                   @  s    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 r   r   Q/home/thesage/.local/lib/python3.10/site-packages/icalendar_searcher/collation.pyr      s    r   c                   @  s   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    r   N	collationcase_sensitiveboolr
   
str | NonereturnCallable[[str, str], bool]c                 C  sr   | t jkr|r	tS tS | t jt jfv r2t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
   r   r   r   get_collation_functionE   s   




r$   Callable[[str], bytes]c                 C  sz   | t jkr|rdd S dd S | t jt jfv r6ts!td|  d| t jkr1|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                 S  s
   |  dS Nzutf-8)encodesr   r   r   <lambda>   s   
 z'get_sort_key_function.<locals>.<lambda>c                 S  s   |   dS r&   )lowerr'   r(   r   r   r   r*      s    r   r   r   Nr   )r   r   r   r   r!   r   _get_icu_sort_keyr#   r   r   r   get_sort_key_functionw   s   




r-   needlestrhaystackc                 C  s   | |v S )z(Binary (case-sensitive) substring match.r   r.   r0   r   r   r   r      s   r   c                 C  s   |   |  v S )z!Case-insensitive substring match.r+   r1   r   r   r   r       s   r    c                   s   d	 fd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.
    r.   r/   r0   r   r   c                   s    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.
        r2   r1   r   r   r   icu_contains   s   z'_get_icu_contains.<locals>.icu_containsNr.   r/   r0   r/   r   r   r   )r
   r   r4   r   r3   r   r"      s   
r"   c                   sN   | rt | nt  }t| |r tj n tj d fd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.
    r)   r/   r   bytesc                   s
     | S )z Generate ICU collation sort key.)
getSortKeyr(   collatorr   r   icu_sort_key   s   
z'_get_icu_sort_key.<locals>.icu_sort_keyN)r)   r/   r   r6   )	ICULocalegetRootICUCollatorcreateInstancesetStrengthTERTIARY	SECONDARY)r
   r   
icu_localer:   r   r8   r   r,      s   
r,   )r   r   r   r   r
   r   r   r   )r   r   r   r   r
   r   r   r%   r5   )r
   r   r   r   r   r   )r
   r   r   r   r   r%   )r   
__future__r   collections.abcr   enumr   icur   r=   r   r;   r!   ImportErrorr/   r   	Exceptionr   r   r$   r-   r   r    r"   r,   r   r   r   r   <module>   s2    (3
7

