"""Helpers for detecting async-populated Salesforce fields in flow conditions."""

from __future__ import annotations

from typing import Iterable, Optional, Set

from ..api.v2.flow_builder_schemas import FlowCondition


def get_async_field_category(field_name: Optional[str]) -> Optional[str]:
    """Return async field category name if field is async-populated."""
    if not field_name:
        return None
    f_lower = field_name.lower()
    if f_lower.startswith("eac_"):
        return "Einstein Activity Capture"
    if "lead_score" in f_lower or "leadscore" in f_lower:
        return "Einstein Lead Scoring"
    if "opportunity_score" in f_lower or "opportunityscore" in f_lower:
        return "Einstein Opportunity Scoring"
    return None


def collect_condition_fields(conditions: Optional[Iterable[FlowCondition]]) -> Set[str]:
    """Collect fields referenced in conditions, including RHS when value_is_field."""
    fields: Set[str] = set()
    if not conditions:
        return fields
    for cond in conditions:
        if cond.field:
            fields.add(cond.field)
        if cond.value_is_field and isinstance(cond.value, str):
            fields.add(cond.value)
    return fields
