<?php

namespace KupShop\GTMBundle\Ecommerce;

class ProductClick extends AbstractEcommerce
{
    /**
     * Specific PageData.
     *
     * @throws \Exception
     */
    public function getData(&$dataContainer)
    {
        $dbcfg = \Settings::getDefault();
        $productOnPage = $dbcfg->cat_show_products;

        $products = $this->productLoader->getProducts([$this->pageData['product'] ?? null], $this->view);

        if ($this->view) {
            $templateData = $this->view->getTemplateData();
            $page = $templateData['body']['pager']->number ?? 1;

            $products[0]->position = $this->pageData['position'] + (($page - 1) * $productOnPage);

        // Když není k dispozici view, tak se bere pozice produktu, která se posílá ze šablony.
        // např. když se AJAXem načítají recommended produkty v košíku #18414
        } elseif (!findModule(\Modules::COMPONENTS) && ($this->pageData['position'] ?? false)) {
            $products[0]->position = $this->pageData['position'];
        }

        $dataContainer->event = 'productClick';
        $dataContainer->click = new \stdClass();
        $dataContainer->click->products = $products;

        $dataContainer->click->listName = $this->productLoader->getListType($this->view, $this->pageData);
        $dataContainer->click->listId = $this->productLoader->getListId($this->view, $this->pageData);
    }
}
