<?php

declare(strict_types=1);

namespace App\Services\Images\Detectors;

use App\Contracts\Images\SubjectDetectorInterface;
use App\Data\Images\SubjectDetectionResultData;

final class ChainedSubjectDetector implements SubjectDetectorInterface
{
    /**
     * @param iterable<int, SubjectDetectorInterface> $detectors
     */
    public function __construct(private readonly iterable $detectors)
    {
    }

    public function detect(string $sourcePath, int $sourceWidth, int $sourceHeight, array $context = []): ?SubjectDetectionResultData
    {
        foreach ($this->detectors as $detector) {
            $result = $detector->detect($sourcePath, $sourceWidth, $sourceHeight, $context);
            if ($result !== null) {
                return $result;
            }
        }

        return null;
    }
}