# #region SupersetUserProjection [C:2] [TYPE Module] [SEMANTICS superset, user, profile, lookup, transform]
# @LAYER Infrastructure
# @BRIEF User/owner payload normalization helpers for Superset client responses.
# @RELATION DEPENDS_ON -> [SupersetClientBase]
from typing import Any


# #region SupersetUserProjectionMixin [C:2] [TYPE Class]
# @BRIEF Mixin providing user/owner payload normalization for Superset API responses.
# @RELATION DEPENDS_ON -> [SupersetClientBase]
class SupersetUserProjectionMixin:
    # #region SupersetClientExtractOwnerLabels [TYPE Function] [C:1]
    # @PURPOSE: Normalize dashboard owners payload to stable display labels.
    def _extract_owner_labels(self, owners_payload: Any) -> list[str]:
        if owners_payload is None:
            return []
        owners_list: list[Any]
        if isinstance(owners_payload, list):
            owners_list = owners_payload
        else:
            owners_list = [owners_payload]
        normalized: list[str] = []
        for owner in owners_list:
            label: str | None = None
            if isinstance(owner, dict):
                label = self._extract_user_display(None, owner)
            else:
                label = self._sanitize_user_text(owner)
            if label and label not in normalized:
                normalized.append(label)
        return normalized
    # #endregion SupersetClientExtractOwnerLabels
    # #region SupersetClientExtractUserDisplay [TYPE Function] [C:1]
    # @PURPOSE: Normalize user payload to a stable display name.
    def _extract_user_display(
        self, preferred_value: str | None, user_payload: dict | None
    ) -> str | None:
        preferred = self._sanitize_user_text(preferred_value)
        if preferred:
            return preferred
        if isinstance(user_payload, dict):
            full_name = self._sanitize_user_text(user_payload.get("full_name"))
            if full_name:
                return full_name
            first_name = self._sanitize_user_text(user_payload.get("first_name")) or ""
            last_name = self._sanitize_user_text(user_payload.get("last_name")) or ""
            combined = " ".join(
                part for part in [first_name, last_name] if part
            ).strip()
            if combined:
                return combined
            username = self._sanitize_user_text(user_payload.get("username"))
            if username:
                return username
            email = self._sanitize_user_text(user_payload.get("email"))
            if email:
                return email
        return None
    # #endregion SupersetClientExtractUserDisplay
    # #region SupersetClientSanitizeUserText [TYPE Function] [C:1]
    # @PURPOSE: Convert scalar value to non-empty user-facing text.
    def _sanitize_user_text(self, value: str | int | None) -> str | None:
        if value is None:
            return None
        normalized = str(value).strip()
        if not normalized:
            return None
        return normalized
    # #endregion SupersetClientSanitizeUserText
# #endregion SupersetUserProjectionMixin
# #endregion SupersetUserProjection
