from __future__ import annotations

FEIMAO_BROWSER_UA = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
)


def looks_like_feimao_session(token: str | None) -> bool:
    if not token:
        return False
    lowered = token.lower()
    if "feemootoken=" in lowered or "feemoo_hw_token=" in lowered:
        return True
    if token.count(";") >= 2 and "=" in token:
        return True
    return len(token.strip()) >= 16


def feimao_request_headers(session_token: str | None, *, with_json: bool = False) -> dict[str, str]:
    headers = {
        "User-Agent": FEIMAO_BROWSER_UA,
        "Origin": "https://www.feemoo.com",
        "Referer": "https://www.feemoo.com/",
        "os": "pc",
        "fm-lang": "zh-CN",
    }
    if with_json:
        headers["Accept"] = "application/json, text/plain, */*"
        headers["Content-Type"] = "application/json"
    if not session_token:
        return headers
    token = session_token.strip()
    if "feemoo_hw_token=" in token.lower() or "feemootoken=" in token.lower() or token.count(";") >= 2:
        headers["Cookie"] = token
        return headers
    headers["feemootoken"] = token
    return headers
