"""URL helpers used by the SQLite store's evidence/image handling.

Extracted from sqlite_store.py to keep that module focused. Behavior is
unchanged. Note these intentionally treat the local-submission suffix set
(SUPPORTED_IMAGE_SUFFIXES, which includes .svg) as image-like; the integration
adapters keep their own stricter suffix policy for external results.
"""

from __future__ import annotations

from pathlib import Path
from urllib.parse import parse_qsl, unquote, urlparse

from rights_filter.server.image_store import SUPPORTED_IMAGE_SUFFIXES


def _decoded_nested_url(value: str) -> str:
    candidate = str(value).strip()
    for _ in range(3):
        decoded = unquote(candidate).strip()
        if decoded == candidate:
            break
        candidate = decoded
    return candidate


def _is_http_url(url: str) -> bool:
    parsed = urlparse(url)
    return parsed.scheme in {"http", "https"} and bool(parsed.netloc)


def _url_path_has_image_suffix(url: str) -> bool:
    return Path(urlparse(url).path).suffix.lower() in SUPPORTED_IMAGE_SUFFIXES


def _url_has_image_format_hint(url: str) -> bool:
    image_formats = {suffix.lstrip(".") for suffix in SUPPORTED_IMAGE_SUFFIXES}
    image_format_keys = {"format", "fm", "ext", "extension", "mime", "output", "type"}
    for key, hint in parse_qsl(urlparse(url).query, keep_blank_values=False):
        if key.lower().replace("-", "_") not in image_format_keys:
            continue
        normalized = hint.lower().split(";", 1)[0].strip().lstrip(".")
        if normalized.startswith("image/"):
            normalized = normalized.split("/", 1)[1]
        normalized = normalized.split("+", 1)[0]
        if normalized in image_formats:
            return True
    return False


def _url_looks_like_image(url: str) -> bool:
    return _url_path_has_image_suffix(url) or _url_has_image_format_hint(url)
