<?php

declare(strict_types=1);

namespace App\Framework\Design\Parser;

use App\Framework\Design\ValueObjects\CustomProperty;

/**
 * Parst CSS Custom Properties (CSS Variables) und konvertiert sie zu Design Tokens
 */
final readonly class CustomPropertyParser
{
    /**
     * Extrahiert alle Custom Properties aus CSS Content
     */
    public function extractFromContent(string $content): array
    {
        $customProperties = [];

        // Regex für CSS Custom Properties: --property-name: value;
        preg_match_all('/--([a-zA-Z0-9_-]+)\s*:\s*([^;]+);/m', $content, $matches, PREG_SET_ORDER);

        foreach ($matches as $match) {
            $propertyName = $match[1];
            $propertyValue = trim($match[2]);

            // CustomProperty erstellen
            $customProperties[] = new CustomProperty($propertyName, $propertyValue);
        }

        return $customProperties;
    }

    /**
     * Extrahiert Custom Properties aus mehreren Dateien
     */
    public function extractFromFiles(array $filePaths): array
    {
        $allProperties = [];

        foreach ($filePaths as $filePath) {
            if (file_exists($filePath)) {
                $content = file_get_contents($filePath);
                $properties = $this->extractFromContent($content);
                $allProperties = array_merge($allProperties, $properties);
            }
        }

        return $allProperties;
    }
}
