<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

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

        // Parse shadow size with optional opacity (e.g., shadow-lg/50, shadow-xl/75)
        if (preg_match('/\bshadow-(sm|md|lg|xl|2xl|inner|none)(?:\/(\d+))?\b/', $style, $matches)) {
            $size = $matches[1];
            if (isset($matches[2])) {
                $opacity = (int)$matches[2];
            }
        }
        // shadow (base shadow) with optional opacity (e.g., shadow/50)
        elseif (preg_match('/\bshadow(?:\/(\d+))?\b/', $style, $matches)) {
            // Only match plain 'shadow' or 'shadow/XX', not 'shadow-color-XXX'
            if (!preg_match('/\bshadow-[a-z]+-\d+/', $style)) {
                $size = 'base';
                if (isset($matches[1])) {
                    $opacity = (int)$matches[1];
                }
            }
        }

        // Parse shadow color (e.g., shadow-red-500, shadow-blue-300)
        if (preg_match('/\bshadow-([a-z]+)-(\d+)(?:\/(\d+))?\b/', $style, $matches)) {
            $colorStr = $matches[1] . '-' . $matches[2];
            $color = Color::parse($colorStr);
            if (isset($matches[3])) {
                $opacity = (int)$matches[3];
            }
            // If color is specified but no size was found, use base shadow
            if ($size === null) {
                $size = 'base';
            }
        }

        // If we still don't have a size, but have color or opacity, default to 'none'
        if ($size === null) {
            $size = 'none';
        }

        // Return shadow style if we found a valid shadow class
        if ($size !== 'none' || $opacity !== null || $color->red >= 0) {
            return new \PHPNative\Tailwind\Style\Shadow($size, $color, $opacity);
        }

        return null;
    }

    public static function merge(\PHPNative\Tailwind\Style\Shadow $class, \PHPNative\Tailwind\Style\Shadow $style)
    {
        // Only merge size if it's not 'base' (which is the default for color-only shadows)
        // or if the existing size is 'none'
        if ($style->size !== 'none' && $style->size !== 'base') {
            $class->size = $style->size;
        } elseif ($style->size === 'base' && $class->size === 'none') {
            // Only set to 'base' if current size is 'none'
            $class->size = $style->size;
        }

        if ($style->color->red >= 0 || $style->color->green >= 0 || $style->color->blue >= 0) {
            // Copy color values instead of reference
            $class->color->red = $style->color->red;
            $class->color->green = $style->color->green;
            $class->color->blue = $style->color->blue;
            $class->color->alpha = $style->color->alpha;
        }
        if ($style->opacity !== null) {
            $class->opacity = $style->opacity;
        }
    }
}
