import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; import "package:nexus/models/per_message_profiles.dart"; part "room_subprofiles.freezed.dart"; @freezed class const RoomSubprofiles({ required final IList profiles, // Each trigger is tried in this order, as gomuks does when sending required final IList triggerOrder, final StoredProfile? current, final StoredProfile? synced, }) with _$RoomSubprofiles { // The subprofile [text] triggers, and what's left once its trigger is removed ({StoredProfile profile, String body})? triggerMatch(String text) => triggerOrder .where((profile) => profile.id.isNotEmpty) .expand( (profile) => profile.triggers .where((trigger) => trigger.matches(text)) .map( (trigger) => ( profile: profile, body: trigger.keepTrigger ? text : text.substring( trigger.prefix.length, text.length - trigger.suffix.length, ), ), ), ) .firstOrNull; StoredProfile? triggeredBy(String text) => triggerMatch(text)?.profile; }