"""Small general-purpose helpers used across the app."""
import re
from flask import request


def wants_json_response():
    """Return JSON only for API/fetch clients, not normal browser navigations."""
    if request.is_json or request.path.startswith("/api/"):
        return True
    accept = request.headers.get("Accept", "")
    requested_with = request.headers.get("X-Requested-With", "")
    return requested_with == "XMLHttpRequest" or (
        "application/json" in accept and "text/html" not in accept
    )


def split_upload_list(value):
    return [item.strip() for item in (value or "").splitlines() if item.strip()]


def amount_value(value):
    """Extract a numeric value from a price string like 'Rs. 960' or 'Rs. 1,200'."""
    m = re.search(r"(\d[\d,]*(?:\.\d+)?)", str(value or ""))
    try:
        return float(m.group(1).replace(",", "")) if m else 0.0
    except (ValueError, AttributeError):
        return 0.0
