| 181 | } |
| 182 | |
| 183 | export const resolveNodeColor = ( |
| 184 | nodeType: string | undefined, |
| 185 | currentMap: Map<string, string> | undefined |
| 186 | ): ResolveNodeColorResult => { |
| 187 | const typeColorMap = currentMap ?? new Map<string, string>() |
| 188 | const normalizedType = nodeType ? nodeType.toLowerCase() : 'unknown' |
| 189 | const standardType = TYPE_SYNONYMS[normalizedType] |
| 190 | const cacheKey = standardType || normalizedType |
| 191 | |
| 192 | if (typeColorMap.has(cacheKey)) { |
| 193 | return { |
| 194 | color: typeColorMap.get(cacheKey) || DEFAULT_NODE_COLOR, |
| 195 | map: typeColorMap, |
| 196 | updated: false |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (standardType) { |
| 201 | const color = NODE_TYPE_COLORS[standardType] || DEFAULT_NODE_COLOR |
| 202 | const newMap = new Map(typeColorMap) |
| 203 | newMap.set(standardType, color) |
| 204 | return { |
| 205 | color, |
| 206 | map: newMap, |
| 207 | updated: true |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | const usedExtendedColors = new Set( |
| 212 | Array.from(typeColorMap.values()).filter((color) => !PREDEFINED_COLOR_SET.has(color)) |
| 213 | ) |
| 214 | |
| 215 | const unusedColor = EXTENDED_COLORS.find((color) => !usedExtendedColors.has(color)) |
| 216 | const color = unusedColor || DEFAULT_NODE_COLOR |
| 217 | |
| 218 | const newMap = new Map(typeColorMap) |
| 219 | newMap.set(normalizedType, color) |
| 220 | |
| 221 | return { |
| 222 | color, |
| 223 | map: newMap, |
| 224 | updated: true |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | export { DEFAULT_NODE_COLOR } |