"""
Template filters for Standards app
"""
from django import template

register = template.Library()


@register.filter
def get_unique(data_list, field_path):
    """
    Get unique values from a list of dictionaries based on a dot-notation path.

    Usage: {{ standards_data|get_unique:"standard.source" }}

    Args:
        data_list: List of dictionaries
        field_path: Dot-separated path to the field (e.g., "standard.source")

    Returns:
        List of unique values for the specified field path
    """
    if not data_list:
        return []

    values = []
    seen = set()

    for item in data_list:
        # Navigate through the dot-notation path
        value = item
        try:
            for attr in field_path.split('.'):
                if value is None:
                    break
                # Handle both dict and object access
                if isinstance(value, dict):
                    value = value.get(attr)
                else:
                    value = getattr(value, attr, None)

            # Only add non-None values that haven't been seen
            if value is not None and value not in seen:
                values.append(value)
                seen.add(value)
        except (AttributeError, KeyError, TypeError):
            # Skip items that don't have the path
            continue

    return values
