<?php

declare(strict_types=1);

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

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

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

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

        return $this;
    }
}
