package org.jabref.logic.integrity;

import java.util.Optional;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.entry.identifier.ISSN;

import org.jspecify.annotations.Nullable;

public class ISSNChecker implements ValueChecker {

    @Override
    public Optional<String> checkValue(@Nullable String value) {
        if (StringUtil.isBlank(value)) {
            return Optional.empty();
        }

        // Check that the ISSN is on the correct form
        String issnString = value.trim();

        ISSN issn = new ISSN(issnString);
        if (!issn.isValidFormat()) {
            return Optional.of(Localization.lang("incorrect format"));
        }

        if (!issn.isValidChecksum()) {
            return Optional.of(Localization.lang("incorrect control digit"));
        }
        return Optional.empty();
    }
}
