<?php

declare(strict_types=1);

namespace Plugin\tnt_stabila_shop\src\Helper;

use JTL\Shop;
use Plugin\tnt_stabila_shop\src\Repositories\DatabaseRepository;
use Plugin\tnt_stabila_shop\src\Repositories\SessionRepository;
use Plugin\tnt_stabila_shop\src\Services\SettingsService;
use ReflectionProperty;
use SQLite3;

class PerformanceBooster
{
    public const MMAP_SIZE = 2147483648;
    public static function getSQLiteCache(): SQLite3|null|false
    {
        $cache          = Shop::Container()->getCache();
        $settingService = new SettingsService(
            new DatabaseRepository(Shop::Container()->getDB()),
            new SessionRepository('tnt_stabila_shop'),
            $cache
        );
        if ($settingService->getPerformanceBoosterEnabled() === false) {
            return false;
        }
        $sqLite = $cache->getActiveMethod();
        try {
            $reflectedDBProperty = new ReflectionProperty($sqLite::class, 'db');
        } catch (\ReflectionException $e) {
            Shop::Container()->getLogService()->debug($e, [self::class]);
            return null;
        }

        return $reflectedDBProperty->getValue($sqLite);
    }
}
