"""批量执行多关键词搜索；每完成一页立即自动落库。"""
from __future__ import annotations

import json
from typing import Any

_SUPPORTED_PROVIDERS = {"internal_keyword", "tikhub"}
_SOURCE_TYPES = {"demand", "seed", "point", "tag", "pagination", "mixed"}
_MAX_SEARCH_TASKS = 6
_MAX_PAGES_PER_TASK = 2


def _load_result(raw: str) -> dict[str, Any]:
    try:
        value = json.loads(raw)
    except (TypeError, ValueError):
        return {"error": "搜索工具返回了无效 JSON", "raw_result": str(raw)}
    return value if isinstance(value, dict) else {"error": "搜索工具返回值不是对象"}


def _positive_page_limit(value: Any) -> int:
    try:
        parsed = int(value)
    except (TypeError, ValueError):
        parsed = 1
    return min(max(parsed, 1), _MAX_PAGES_PER_TASK)
