package org.jabref.gui.maintable;

import org.jabref.model.entry.AuthorList;
import org.jabref.preferences.PreferencesService;

public class MainTableNameFormatter {

    private final PreferencesService preferencesService;

    MainTableNameFormatter(PreferencesService preferences) {
        this.preferencesService = preferences;
    }

    /**
     * Format a name field for the table, according to user preferences.
     *
     * @param nameToFormat The contents of the name field.
     * @return The formatted name field.
     */
    public String formatName(final String nameToFormat) {
        if (nameToFormat == null) {
            return null;
        }

        MainTableNameFormatPreferences nameFormatPreferences = preferencesService.getMainTableNameFormatPreferences();
        MainTableNameFormatPreferences.DisplayStyle displayStyle = nameFormatPreferences.getDisplayStyle();
        MainTableNameFormatPreferences.AbbreviationStyle abbreviationStyle = nameFormatPreferences.getAbbreviationStyle();

        if (((displayStyle == MainTableNameFormatPreferences.DisplayStyle.FIRSTNAME_LASTNAME)
                || (displayStyle == MainTableNameFormatPreferences.DisplayStyle.LASTNAME_FIRSTNAME))
                && abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.LASTNAME_ONLY) {
            return AuthorList.fixAuthorLastNameOnlyCommas(nameToFormat, false);
        }

        return switch (nameFormatPreferences.getDisplayStyle()) {
            case AS_IS -> nameToFormat;
            case NATBIB -> AuthorList.fixAuthorNatbib(nameToFormat);
            case FIRSTNAME_LASTNAME -> AuthorList.fixAuthorFirstNameFirstCommas(
                    nameToFormat,
                    abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.FULL,
                    false);
            case LASTNAME_FIRSTNAME -> AuthorList.fixAuthorLastNameFirstCommas(
                    nameToFormat,
                    abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.FULL,
                    false);
        };
    }
}

@@ -1410,23 +1410,16 @@
         
-return 
 switch (
@@ -1483,19 +1483,40 @@
 se AS_IS
- -%3E
+:%0A                return
  nameToF
@@ -1545,19 +1545,40 @@
 e NATBIB
- -%3E
+:%0A                return
  AuthorL
@@ -1643,27 +1643,48 @@
 AME_LASTNAME
- -%3E
+:%0A                return
  AuthorList.
@@ -1726,32 +1726,36 @@
                 
+    
 nameToFormat,%0A  
@@ -1744,32 +1744,36 @@
   nameToFormat,%0A
+    
                 
@@ -1868,32 +1868,36 @@
             
+    
 false);%0A
             
@@ -1876,32 +1876,53 @@
         false);%0A
+            default:%0A
             case
@@ -1944,11 +1944,32 @@
 NAME
- -%3E
+:%0A                return
  Aut
@@ -2018,32 +2018,36 @@
                 
+    
 nameToFormat,%0A  
@@ -2036,32 +2036,36 @@
   nameToFormat,%0A
+    
                 
@@ -2156,32 +2156,36 @@
                 
+    
 false);%0A        
@@ -2185,17 +2185,16 @@
        %7D
-;
 %0A    %7D%0A%7D

Revert to old switch expression for checkstyle compatibility