from __future__ import annotations

import re
from urllib.parse import urlparse


def is_valid_color(color_string: str) -> bool:
    return bool(re.fullmatch(r"#[0-9a-fA-F]{6}", color_string or ""))


def is_valid_bundle_id(bundle_id: str) -> bool:
    return bool(
        re.fullmatch(r"[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+", bundle_id or "")
    )


def is_valid_relative_or_absolute_url(value: str) -> bool:
    if not value:
        return False
    if value.startswith("/"):
        return True
    parsed = urlparse(value)
    return bool(parsed.scheme and parsed.netloc)


def normalize_path(value: str, default: str = "/") -> str:
    if not value:
        return default
    if value.startswith("http://") or value.startswith("https://"):
        parsed = urlparse(value)
        return parsed.path or "/"
    if value.startswith("/"):
        return value
    return f"/{value}"
