from matchms.importing import load_from_msp
from matchms.filtering import default_filters, normalize_intensities


def load_spectra_with_intersection(annotated, raw):
    annotated = _load_spectrum(annotated)
    raw = _load_spectrum(raw)

    names = _find_names_intersection(
        {s.metadata["compound_name"] for s in annotated},
        {s.metadata["compound_name"] for s in raw}
    )

    return annotated, names


def load_spectrum(spectrum):
    spectrum = list(load_from_msp(spectrum))
    spectrum = list(map(default_filters, spectrum))
    spectrum = list(map(normalize_intensities, spectrum))
    return spectrum


def _find_names_intersection(annotated: set, raw: set):
    return annotated & raw
