<?php

namespace App\Services\Factories;

use App\Contracts\HomepageParserInterface;
use App\Models\Feed;

class HomepageParserFactory
{
    public static function getParserForFeed(Feed $feed): ?HomepageParserInterface
    {
        if (! $feed->provider) {
            return null;
        }

        $providerConfig = config("feed.providers.{$feed->provider}");
        if (! $providerConfig || ! isset($providerConfig['parsers']['homepage'])) {
            return null;
        }

        /** @var class-string<HomepageParserInterface> $parserClass */
        $parserClass = $providerConfig['parsers']['homepage'];
        if (! class_exists($parserClass)) {
            return null;
        }

        $language = $feed->language->short_code ?? 'en';

        return new $parserClass($language);
    }
}
