<?php

namespace KupShop\GTMBundle\Ecommerce;

class ClickRemoveFromCart extends AbstractEcommerce
{
    /**
     * Specific PageData.
     *
     * @throws \Exception
     */
    public function getData(&$dataContainer)
    {
        $addedProduct = $this->productLoader->getProducts([$this->pageData['product']], $this->view);
        if (findModule(\Modules::COMPONENTS)) {
            $dataContainer->event = 'remove_from_cart';
            $fk = array_key_first($addedProduct);
            $addedProduct[$fk]['quantity'] = $this->pageData['quantity'] ?: 1;
            $dataContainer->ecommerce = (object) [
                'items' => (object) [
                    $addedProduct[$fk],
                ],
            ];
        } else {
            $dataContainer->event = 'removeFromCart';
            $dataContainer->remove = new \stdClass();
            $dataContainer->remove->products = $addedProduct;
            $addedProduct[0]->quantity = $this->pageData['quantity'] ?: 1;
        }
        $dataContainer->_clear = true;
    }
}
