<?php

declare(strict_types=1);

namespace App\Framework\Svg\ValueObjects\Styling;

/**
 * Value Object representing SVG fill styling
 */
final readonly class Fill
{
    public function __construct(
        public SvgColor $color,
        public Opacity $opacity = new Opacity(1.0)
    ) {
    }

    public static function none(): self
    {
        return new self(SvgColor::transparent(), new Opacity(0.0));
    }

    public static function solid(SvgColor $color): self
    {
        return new self($color, new Opacity(1.0));
    }

    public static function withOpacity(SvgColor $color, float $opacity): self
    {
        return new self($color, new Opacity($opacity));
    }

    /**
     * Check if fill is visible
     */
    public function isVisible(): bool
    {
        return $this->opacity->value > 0 && !$this->color->isTransparent();
    }

    /**
     * Convert to SVG attributes
     */
    public function toSvgAttributes(): array
    {
        $attributes = ['fill' => $this->color->toSvgValue()];

        if ($this->opacity->value < 1.0) {
            $attributes['fill-opacity'] = $this->opacity->toSvgValue();
        }

        return $attributes;
    }

    public function toArray(): array
    {
        return [
            'color' => $this->color->toSvgValue(),
            'opacity' => $this->opacity->value,
            'visible' => $this->isVisible(),
        ];
    }
}
