"""Json abstraction functions."""

import json
from typing import Any


def get_json_value(json_path: str, dictionary: str, key: str) -> Any:
    """Read a value from a json file.

    Args:
    ----
        json_path (str): Path to the json file.
        dictionary (str): Dictionary name the value is in.
        key (str): Key in the dictionary.

    Returns:
    -------
        Any: Value from the json file.

    """
    with open(json_path, "r") as f:
        json_obj = json.load(f)

    if dictionary not in json_obj or key not in json_obj[dictionary]:
        return None

    return json_obj[dictionary][key]


def set_json_value(json_path: str, dictionary: str, key: str, value: Any) -> None:
    """Set a value in a json file.

    Args:
    ----
        json_path (str): Path to the json file.
        dictionary (str): Dictionary name the value is in.
        key (str): Key in the dictionary.
        value (Any): Value to set.

    """
    with open(json_path, "r") as f:
        json_obj = json.load(f)

    json_obj[dictionary][key] = value

    with open(json_path, "w") as f:
        json.dump(json_obj, f)


def delete_json_dictionary(json_path: str, dictionary: str) -> None:
    """Delete a dictionary in a json file.

    Args:
    ----
        json_path (str): Path to the json file.
        dictionary (str): Dictionary name to delete.

    """
    with open(json_path, "r") as f:
        data = json.load(f)

    if dictionary in data:
        del data[dictionary]

    # Save the updated JSON data back to the file
    with open("output.json", "w") as f:
        json.dump(data, f)


def clear_json_file(json_path: str) -> None:
    """Clear a json file.

    Args:
    ----
        json_path (str): Path to the json file.

    """
    with open(json_path, "w") as f:
        json.dump({}, f)
