use std::collections::HashMap;
use super::graph::GraphNode;
const KIND_SLOT: [(&str, usize); 7] = [
("tool", 0),
("task", 1),
("decision", 2),
("input", 3),
("refdoc", 4),
("connector", 5),
("action", 6),
];
pub(crate) const KIND_COUNT: usize = KIND_SLOT.len();
pub(crate) fn kind_slot(kind: &str) -> Option<usize> {
KIND_SLOT.iter().find(|(k, _)| *k == kind).map(|&(_, s)| s)
}
fn container_slot_count(palette_length: usize) -> usize {
palette_length.saturating_sub(KIND_COUNT).max(1)
}
fn container_slot(n: usize, palette_length: usize) -> usize {
let slot = KIND_COUNT + n % container_slot_count(palette_length);
if palette_length > 0 {
slot % palette_length
} else {
slot
}
}
pub(crate) fn assign_color_slots(
nodes: &mut [GraphNode],
kind_of: impl Fn(&str) -> Option<&'static str>,
container_order: &HashMap<String, usize>,
palette_length: usize,
) {
let mut fallback = container_order.len();
for node in nodes {
if node.is_group || node.shape == "collapsedGroup" {
let n = match container_order.get(&node.id) {
Some(&n) => n,
None => {
fallback += 1;
fallback - 1
}
};
node.color_index = Some(container_slot(n, palette_length));
continue;
}
if let Some(kind) = kind_of(&node.id).filter(|k| kind_slot(k).is_some()) {
node.kind = Some(kind);
}
}
}