# Copyright 2025 © BeeAI a Series of LF Projects, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from collections.abc import Mapping
from typing import Any


def exclude_keys(input: dict[str, Any], keys: set[str]) -> dict[str, Any]:
    return {k: input[k] for k in input.keys() - keys}


def include_keys(input: dict[str, Any], keys: set[str]) -> dict[str, Any]:
    valid_keys = [k for k in input if k in keys]
    return {k: input[k] for k in valid_keys}


def exclude_none(input: dict[str, Any]) -> dict[str, Any]:
    return {k: v for k, v in input.items() if v is not None}


def exclude_non_annotated(input: Mapping[str, Any], cls: type[Mapping[str, Any]]) -> dict[str, Any]:
    if not isinstance(input, dict):
        raise ValueError("input must be a TypedDict")

    excluded: dict[str, Any] = {}

    valid_keys = cls.__annotations__.keys()
    for k, v in list(input.items()):
        if k not in valid_keys:
            excluded[k] = v
            input.pop(k)

    return excluded
