formatStatementFromRecap static method

String formatStatementFromRecap({
  1. String statement = '',
  2. Map<String, dynamic> recap = const {},
})

Implementation

static String formatStatementFromRecap({
  String statement = '',
  Map<String, dynamic> recap = const {},
}) {
  isValidRecap(recap);
  //
  final baseStatement =
      'I further authorize the stated URI to perform the following actions on my behalf: ';
  if (statement.contains(baseStatement)) return statement;
  //
  final List<String> statementForRecap = <String>[];
  int currentCounter = 0;
  final att = recap['att'] as Map<String, dynamic>;
  final resources = att.keys;
  for (var resource in resources) {
    final abilities = att[resource];
    final resourceAbilities = (abilities as Map<String, dynamic>).keys;
    final actions = resourceAbilities.map((ability) {
      return {
        'ability': ability.split('/')[0],
        'action': ability.split('/')[1],
      };
    }).toList();
    actions.sort((a, b) => a['action']!.compareTo(b['action']!));
    //
    final uniqueAbilities = <String, dynamic>{};
    for (var actionMap in actions) {
      final ability = actionMap['ability']!;
      final action = actionMap['action']!;
      if (uniqueAbilities[ability] == null) {
        uniqueAbilities[ability] = [];
      }
      uniqueAbilities[ability].add(action);
    }
    //
    final abilitiesStatements = uniqueAbilities.keys.map((ability) {
      currentCounter++;
      final abilities = (uniqueAbilities[ability] as List).join('\', \'');
      return '($currentCounter) \'$ability\': \'$abilities\' for \'$resource\'.';
    }).toList();

    statementForRecap.add(
      abilitiesStatements.join(', ').replaceAll('.,', '.'),
    );
  }
  //
  final recapStatemet = statementForRecap.join(' ');
  final recapStatement = '$baseStatement$recapStatemet';
  // add a space if there is a statement
  return '${statement.isNotEmpty ? "$statement " : ""}$recapStatement';
}