<?php

declare(strict_types=1);

namespace Gls\GlsPoland\Configuration\Initializer;

use Twig\Loader\FilesystemLoader;

final class TwigTemplatePathsInitializer implements ConfigurationInitializerInterface
{
    /**
     * @var FilesystemLoader
     */
    private $loader;

    /**
     * @var \Module
     */
    private $module;

    public function __construct(FilesystemLoader $loader, \Module $module)
    {
        $this->loader = $loader;
        $this->module = $module;
    }

    public function init(): void
    {
        $path = rtrim($this->module->getLocalPath(), '/\\') . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'PrestaShop';
        $namespace = 'PrestaShop';

        if (!is_dir($path)) {
            return;
        }

        if (in_array($path, $this->loader->getPaths($namespace), true)) {
            return;
        }

        $this->loader->prependPath($path, $namespace);
    }
}
