from __future__ import annotations

from collections.abc import Hashable
from typing import cast

import networkx as nx

from graph.exploration.algorithms.in_memory.graph_features.common import FeatureRowsByKey


def apply_closeness(rows_by_key: FeatureRowsByKey, graph: nx.Graph | nx.DiGraph) -> None:
    for node_key, value in cast(dict[Hashable, float], nx.closeness_centrality(graph)).items():
        rows_by_key[node_key]["closeness_centrality"] = float(value)
