<?php

declare(strict_types=1);

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

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

/**
 * Class PocetKontrolPodlaEmisnehoSystemuVozidlaEChartSource
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Pracovisko\PocetKontrolPodlaEmisnehoSystemuVozidla
 */
final class PocetKontrolPodlaEmisnehoSystemuVozidlaEChartSource 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->emisnySystem()->equals($sourceItem->emisnySystem())) {
                    $existItem->incrementData($sourceItem->emisnySystem(), $sourceItem->pocet());
                    $addIt = false;
                    break;
                }
            }
        }

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

        return $this;
    }
}
