<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-cache-warmer
 * @version   1.11.13
 * @copyright Copyright (C) 2026 Mirasvit (https://mirasvit.com/)
 */



namespace Mirasvit\CacheWarmer\Block;

use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\View\Element\Template;
use Mirasvit\CacheWarmer\Service\Config\ExtendedConfig;
use Mirasvit\CacheWarmer\Model\Config;

class TrackJs extends Template
{
    const COOKIE = 'mst-cache-warmer-track';

    private $cookieManager;

    private $urlBuilder;

    private $extendedConfig;

    private $moduleConfig;

    public function __construct(
        CookieManagerInterface $cookieManager,
        ExtendedConfig $extendedConfig,
        Config $config,
        Context $context
    ) {
        $this->cookieManager  = $cookieManager;
        $this->urlBuilder     = $context->getUrlBuilder();
        $this->extendedConfig = $extendedConfig;
        $this->moduleConfig   = $config;

        parent::__construct($context);
    }

    /**
     * @return array
     */
    public function getMageInit()
    {
        /** @var \Magento\Framework\App\Request\Http $request */
        $request = $this->getRequest();

        $cookie = (int)$this->cookieManager->getCookie(self::COOKIE);

        return [
            'Mirasvit_CacheWarmer/js/track' => [
                'pageType'    => $request->getFullActionName(),
                'url'         => $this->urlBuilder->getUrl('cache_warmer/track'),
                'cookieName'  => self::COOKIE,
                'cookieValue' => $cookie > 0 ? $cookie : null,
            ],
        ];
    }

    public function toHtml()
    {
        if (!$this->moduleConfig->isEnabled()) {
            return '';
        }

        if (!$this->extendedConfig->isStatisticsEnabled()) {
            return '';
        }

        if (str_contains(
            strtolower($this->getRequest()->getModuleName()), 'checkout')
        ) {
            return '';
        }

        return parent::toHtml();
    }
}
