#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

from typing import Any, Mapping, Optional, Sequence

# TODO: Cleanup this separation violation. This should be accessed through the check API
from cmk.base.config import SpecialAgentConfiguration


def agent_bi_arguments(
    params: Sequence[Mapping[str, Any]], hostname: str, ipaddress: Optional[str]
) -> SpecialAgentConfiguration:
    # There is an inconsistency between the WATO rule and the webapi.
    # WATO <-> API
    #  aggr_groups / aggr_group_prefix <-> groups
    #  aggr_name_regex / aggr_name <-> names
    # Note: In 1.6 aggr_name_regex never worked as regex, it always was an exact match
    for param_set in params:
        filter_ = param_set.get("filter", {})
        for replacement, name in (
            ("groups", "aggr_groups"),  # 1.6 (deprecated)
            ("names", "aggr_name_regex"),  # 1.6 (deprecated)
            ("groups", "aggr_group_prefix"),
            ("names", "aggr_name"),
        ):
            if name in filter_:
                filter_[replacement] = filter_.pop(name)

    return SpecialAgentConfiguration([], repr(params))


special_agent_info["bi"] = agent_bi_arguments
