findFor method
First executor (by descending priority, then registration order) whose
SkillExecutorProvider.canExecute accepts skillType; null if none.
Implementation
SkillExecutorProvider? findFor(String skillType) {
final matches = _registered.where((e) => e.canExecute(skillType)).toList();
if (matches.isEmpty) return null;
// Composite-key sort (Dart's List.sort is NOT stable): descending priority,
// then ascending original index so first-registered wins on equal priority.
final indexed = [for (var i = 0; i < matches.length; i++) (i, matches[i])];
indexed.sort((a, b) {
final byPriority = b.$2.priority.compareTo(a.$2.priority);
return byPriority != 0 ? byPriority : a.$1.compareTo(b.$1);
});
if (kDebugMode &&
indexed.length > 1 &&
indexed[0].$2.priority == indexed[1].$2.priority) {
edgeAiLog(
'[flutter_edge_ai] Ambiguous skill executor: '
'${indexed.map((e) => e.$2.name).join(", ")} all handle "$skillType" at '
'priority ${indexed[0].$2.priority}; using "${indexed[0].$2.name}" '
'(first registered).',
);
}
return indexed.first.$2;
}