"""Build list/retrieve query parameters."""

from __future__ import annotations


class QueryBuilder:
    @staticmethod
    def _normalize_tags(tags: list[str] | None) -> list[str]:
        return [tag.strip() for tag in (tags or []) if tag and tag.strip()]

    def build_list_params(self, tags: list[str] | None = None, query: str | None = None) -> dict:
        params: dict[str, object] = {}
        normalized = self._normalize_tags(tags)
        if normalized:
            params["keywords"] = " ".join(f"[{tag}]" for tag in normalized)
        if query and query.strip():
            params["keywords"] = f"{params.get('keywords', '')} {query.strip()}".strip()
        return params

    def build_retrieve_params(
        self,
        dataset_ids: list[str],
        query: str | None = None,
        tags: list[str] | None = None,
    ) -> dict:
        params: dict[str, object] = {"dataset_ids": dataset_ids}
        if query and query.strip():
            params["question"] = query.strip()
        normalized = self._normalize_tags(tags)
        if normalized:
            params["metadata_condition"] = {
                "logic": "and",
                "conditions": [
                    {
                        "name": "tags",
                        "comparison_operator": "contains",
                        "value": tag,
                    }
                    for tag in normalized
                ],
            }
        return params
