<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolPodlaKategorieVozidla;

use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceAbstract;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract;

/**
 * Class PocetKontrolPodlaKategorieVozidlaEChartSource
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolPodlaKategorieVozidla
 */
final class PocetKontrolPodlaKategorieVozidlaEChartSource extends EChartSourceAbstract
{
    /**
     * @param \Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\EChartSourceItemContract $sourceItem
     *
     * @return $this
     */
    public function addItem(EChartSourceItemContract $sourceItem): self
    {
        $addIt = true;

        if (!empty($this->items)) {
            foreach ($this->items as $existItem) {
                if ($existItem->kategoriaVozidla()->equals($sourceItem->kategoriaVozidla())) {
                    $existItem->incrementData($sourceItem->kategoriaVozidla(), $sourceItem->pocet());
                    $addIt = false;
                    break;
                }
            }
        }

        if ($addIt) $this->items[] = $sourceItem;

        return $this;
    }
}
