<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

class Background implements Parser
{
    public static function parse(string $style): ?\PHPNative\Tailwind\Style\Background
    {
        $color = new \PHPNative\Tailwind\Style\Color();

        // Nur den ersten bg-* Token ohne nachfolgende Klassen extrahieren
        // Beispiel: "bg-lime-300 border ..." -> "lime-300"
        preg_match_all('/bg-([^\s]+)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $colorStyle = $output_array[1][0];

            // Skip gradient-related classes (gradient directions and color stops)
            // These require special handling that's not yet implemented
            if (str_starts_with($colorStyle, 'gradient-') ||
                str_starts_with($colorStyle, 'from-') ||
                str_starts_with($colorStyle, 'to-') ||
                str_starts_with($colorStyle, 'via-')) {
                return null;
            }

            $color = Color::parse($colorStyle);
            return new \PHPNative\Tailwind\Style\Background($color);
        }

        return null;
    }
}
