"""Utilities to help with integration of BeerCSS with FastHTML"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_foundations.ipynb.

# %% auto #0
__all__ = ['VEnum', 'stringify', 'normalize_tokens', 'dedupe_preserve_order']

# %% ../nbs/00_foundations.ipynb #f1c2465a
from typing import Any, Iterable, Optional, Union
from enum import Enum
from fastcore.utils import *
from nbdev.showdoc import show_doc

# %% ../nbs/00_foundations.ipynb #4411c74b
#| code-fold: true
#| code-fold: true
class VEnum(Enum):
    """Enum with string conversion and concatenation support"""
    def __str__(self): return self.value
    def __add__(self, other): return stringify((self, other))
    def __radd__(self, other): return stringify((other, self))

# %% ../nbs/00_foundations.ipynb #f4cac5a4
#| code-fold: true
#| code-fold: true
def stringify(o):
    """Converts input types into strings that can be passed to FT components"""
    if is_listy(o): 
        return ' '.join(map(str, o)) if o else ""
    return str(o)

# %% ../nbs/00_foundations.ipynb #865963c6
#| code-fold: true
#| code-fold: true
def normalize_tokens(cls):
    """Normalize class input to list of string tokens"""
    if cls is None:
        return []
    if isinstance(cls, str):
        return cls.split()
    if isinstance(cls, Enum):
        return [str(cls)]
    if is_listy(cls):
        tokens = []
        for item in cls:
            if isinstance(item, str):
                tokens.extend(item.split())
            elif isinstance(item, Enum):
                tokens.append(str(item))
        return tokens
    return []


def dedupe_preserve_order(tokens):
    """Remove duplicates while preserving order"""
    seen = set()
    result = []
    for token in tokens:
        if token not in seen:
            seen.add(token)
            result.append(token)
    return result
