"""Input validation utilities."""

from __future__ import annotations

from ._constants import MIN_API_KEY_LENGTH
from ._exceptions import ValidationError


def validate_api_key(api_key: str | None) -> str:
    """Validate API key format and return it if valid. Returns empty string for local usage."""
    if api_key is None:
        return ""

    if not isinstance(api_key, str):
        raise ValidationError("API key must be a string")

    api_key = api_key.strip()
    if not api_key:
        return ""

    if len(api_key) < MIN_API_KEY_LENGTH:
        raise ValidationError(f"API key must be at least {MIN_API_KEY_LENGTH} characters")

    if " " in api_key:
        raise ValidationError("API key cannot contain spaces")

    return api_key
