<?php

declare(strict_types=1);

namespace App\Framework\Svg\ValueObjects\Transform;

/**
 * Value Object representing SVG translate transformation
 */
final readonly class Translation
{
    public function __construct(
        public float $x,
        public float $y = 0.0
    ) {
    }

    /**
     * Convert to SVG transform function
     */
    public function toSvgString(): string
    {
        if ($this->y === 0.0) {
            return sprintf('translate(%.2f)', $this->x);
        }

        return sprintf('translate(%.2f %.2f)', $this->x, $this->y);
    }

    public function toArray(): array
    {
        return [
            'type' => 'translate',
            'x' => $this->x,
            'y' => $this->y,
        ];
    }
}
