package at.procon.dip.extraction.service;

import at.procon.dip.extraction.spi.DocumentExtractor;
import at.procon.dip.extraction.spi.ExtractionRequest;
import at.procon.dip.extraction.spi.ExtractionResult;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DocumentExtractionService {

    private final List<DocumentExtractor> extractors;

    public ExtractionResult extract(ExtractionRequest extractionRequest) {
        return extractors.stream()
                .filter(extractor -> extractor.supports(
                        extractionRequest.detectionResult().documentType(),
                        extractionRequest.detectionResult().mimeType()))
                .findFirst()
                .orElseThrow(() -> new IllegalStateException(
                        "No extractor registered for type " + extractionRequest.detectionResult().documentType()))
                .extract(extractionRequest);
    }
}
