<?php

declare(strict_types=1);

namespace App\Knowledge\Extractor;

final class ExtractorResolver
{
    /**
     * @param iterable<DocumentExtractorInterface> $extractors
     */
    public function __construct(
        private iterable $extractors
    ) {
    }

    public function extract(string $path): string
    {
        $extension = pathinfo($path, PATHINFO_EXTENSION);

        foreach ($this->extractors as $extractor) {
            if ($extractor->supports($extension)) {
                return $extractor->extract($path);
            }
        }

        throw new \RuntimeException(sprintf(
            'No extractor available for extension "%s".',
            $extension
        ));
    }
}
