"""Shared utilities for Purrio CLI."""

import sys

from rich.console import Console

from .config import Config, OutputFormat, load_config
from .output import OutputFormatter

console = Console()
error_console = Console(stderr=True)


def complete_coin_symbol() -> list[str]:
    """Provide coin symbol completion."""
    return [
        "BTC",
        "ETH",
        "SOL",
        "XRP",
        "ADA",
        "DOGE",
        "DOT",
        "AVAX",
        "LINK",
        "MATIC",
        "UNI",
        "ATOM",
        "LTC",
        "BCH",
        "XLM",
        "ALGO",
        "FTM",
        "MANA",
        "SAND",
        "APE",
        "TRUMP",
        "TAO",
        "RNDR",
        "NEAR",
        "OP",
        "ARB",
        "BLUR",
        "MKR",
        "LDO",
        "CRV",
    ]


def get_config() -> Config:
    """Load configuration with error handling."""
    try:
        return load_config()
    except Exception as e:
        error_console.print(f"[red]Error loading configuration:[/red] {e}")
        sys.exit(1)


def get_output_formatter(output_format: OutputFormat | None = None) -> OutputFormatter:
    """Get output formatter with specified or default format."""
    config = get_config()
    format = output_format or config.output_format
    return OutputFormatter(format)
