"""Batch processing module with async interface."""


async def async_batch_process(items: list[dict]) -> list[dict]:
    """Process a batch of items, deduplicating by ID.

    Intended as the handler behind an async endpoint. Deduplicates items
    by ID (keeping first occurrence), processes each, and returns results
    sorted by priority descending.
    """
    unique = _deduplicate(items)
    results = [_transform(item) for item in unique]
    results.sort(key=lambda x: x["priority"], reverse=True)
    return results


def _deduplicate(items: list[dict]) -> list[dict]:
    """Remove duplicate items by ID, keeping first occurrence."""
    unique = []
    seen = []
    for item in items:
        if item["id"] not in seen:
            seen.append(item["id"])
            unique.append(item)
    return unique


def _transform(item: dict) -> dict:
    """Apply transformation to a single item."""
    return {
        "id": item["id"],
        "data": item["data"].upper()
        if isinstance(item["data"], str)
        else item["data"],
        "priority": item["priority"],
        "processed": True,
    }
