<?php

declare(strict_types=1);

namespace App\Framework\View\Components;

use App\Framework\View\ValueObjects\HtmlAttributes;
use App\Framework\View\ValueObjects\StandardHtmlElement;
use App\Framework\View\ValueObjects\TagName;

final readonly class PictureSource
{
    public function __construct(
        public string $srcset,
        public ?string $type = null,
        public ?string $media = null,
        public ?string $sizes = null
    ) {
    }

    public static function webp(string $srcset, ?string $media = null): self
    {
        return new self(srcset: $srcset, type: 'image/webp', media: $media);
    }

    public static function avif(string $srcset, ?string $media = null): self
    {
        return new self(srcset: $srcset, type: 'image/avif', media: $media);
    }

    public static function jpeg(string $srcset, ?string $media = null): self
    {
        return new self(srcset: $srcset, type: 'image/jpeg', media: $media);
    }

    public static function png(string $srcset, ?string $media = null): self
    {
        return new self(srcset: $srcset, type: 'image/png', media: $media);
    }

    public static function responsive(
        string $srcset,
        string $media,
        ?string $type = null
    ): self {
        return new self(srcset: $srcset, type: $type, media: $media);
    }

    public function __toString(): string
    {
        $attributes = HtmlAttributes::empty()
            ->with('srcset', $this->srcset);

        if ($this->type !== null) {
            $attributes = $attributes->with('type', $this->type);
        }

        if ($this->media !== null) {
            $attributes = $attributes->with('media', $this->media);
        }

        if ($this->sizes !== null) {
            $attributes = $attributes->with('sizes', $this->sizes);
        }

        return (string) StandardHtmlElement::create(TagName::SOURCE, $attributes);
    }
}
