package click.kamil.springstatemachineexporter.analysis.index;

/**
 * Shared JavaBean accessor naming helpers used by analysis and dataflow code.
 */
public final class AccessorNaming {

    private AccessorNaming() {
    }

    public static boolean isBeanStyleAccessorName(String accessorName) {
        return (accessorName.startsWith("get") && accessorName.length() > 3)
                || (accessorName.startsWith("is") && accessorName.length() > 2)
                || (accessorName.startsWith("set") && accessorName.length() > 3);
    }

    public static String propertyNameFromAccessor(String accessorName) {
        if (accessorName.startsWith("get") && accessorName.length() > 3) {
            return TrivialAccessorDetector.decapitalize(accessorName.substring(3));
        }
        if (accessorName.startsWith("is") && accessorName.length() > 2) {
            return TrivialAccessorDetector.decapitalize(accessorName.substring(2));
        }
        if (accessorName.startsWith("set") && accessorName.length() > 3) {
            return TrivialAccessorDetector.decapitalize(accessorName.substring(3));
        }
        return accessorName.startsWith("get") ? accessorName.substring(3) : accessorName;
    }

    public static String setterMethodNameForGetter(String getterName) {
        return "set" + capitalizeProperty(propertyNameFromAccessor(getterName));
    }

    public static String capitalizeProperty(String propertyName) {
        if (propertyName == null || propertyName.isEmpty()) {
            return propertyName;
        }
        if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
            return propertyName;
        }
        return Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    }

    public static boolean looksLikeIndexedAccessorName(String methodName) {
        return methodName.startsWith("get") || methodName.startsWith("is") || methodName.startsWith("set");
    }
}
