<?php

declare(strict_types=1);

namespace KupShop\ArticlesBundle\Twig\Components\ArticlesList;

use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;

class Item extends BaseComponent
{
    public array $article;
    public bool $lazyImg = false;

    public function getTitle(): string
    {
        return $this->article['title'] ?? '';
    }

    public function getUrl(): string
    {
        return path('kupshop_content_articles_article_1', [
            'IDa' => $this->article['id'],
            'slug' => $this->article['seo_url'],
        ]);
    }

    public function getThumbnail(): ?Thumbnail
    {
        if (!empty($this->article['photoId'])) {
            return new Thumbnail((string) $this->article['photoId'], $this->article['photoDescr'], (string) $this->article['photoDateUpdate']);
        }

        if (!empty($this->article['image'])) {
            return new Thumbnail((string) $this->article['image']['id'], $this->article['image']['descr'], (string) $this->article['image']['date_update']);
        }

        return null;
    }

    public function getLead(): string
    {
        return $this->article['lead_in'] ?? '';
    }

    public function getDate(): string
    {
        return $this->article['date']?->format(\Settings::getDateFormat());
    }
}
