package org.jabref.logic.integrity;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.database.BibDatabaseContext;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

public class NoteChecker implements ValueChecker {

    private static final Predicate<String> FIRST_LETTER_CAPITALIZED = Pattern.compile("^[^a-z]").asPredicate();

    private final BibDatabaseContext bibDatabaseContextEdition;

    public NoteChecker(@NonNull BibDatabaseContext bibDatabaseContext) {
        this.bibDatabaseContextEdition = bibDatabaseContext;
    }

    /// biblatex package documentation (Section 4.9.1):
    /// The biblatex package will automatically capitalize the first word when required at the beginning of a sentence.
    /// Official BibTeX specification:
    /// note: Any additional information that can help the reader. The first word should be capitalized.
    @Override
    public Optional<String> checkValue(@Nullable String value) {
        if (StringUtil.isBlank(value)) {
            return Optional.empty();
        }

        // BibTeX
        if (!bibDatabaseContextEdition.isBiblatexMode() && !FIRST_LETTER_CAPITALIZED.test(value.trim())) {
            return Optional.of(Localization.lang("should have the first letter capitalized"));
        }

        return Optional.empty();
    }
}
