<?php

namespace ProudNerds\Plugins;

use ProudNerds\Trait\Initializable;
use Timber\Site;
use Timber\Timber;

/**
 * Class PluginAdvanceCustomFields
 */
class LocalTimber
{
    use Initializable;

    /**
     * PluginAdvanceCustomFields constructor.
     */
    public function __construct()
    {
        $this->addFilters();
    }

    /**
     * Adds items to global Timber context
     * @return array with Timber context
     */
    public function addTimberContext(): array
    {
        // Set Twig template directories
        Timber::$dirname = ['views', 'blocks'];

        $context['env'] = WP_ENV;
        $context['body_class'] = implode(' ', get_body_class());
        $context['options'] = get_fields('theme-options');
        $context['site'] = new Site();

        $context['main_menu'] = Timber::get_menu('main-menu');

        $context['footer_menu'] = Timber::get_menu('footer');

        $theme_folder = get_stylesheet_directory_uri();
        $context['site']->assets = [
            'icons' => $theme_folder . '/assets/icons',
            'img' => $theme_folder . '/assets/img',
            'js' => $theme_folder . '/assets/js',
        ];

        if (function_exists('yoast_breadcrumb')) {
            $context['breadcrumbs'] = yoast_breadcrumb('', '', false);
        }
        return $context;
    }

    private function addFilters(): void
    {
        add_filter('timber/context', [$this, 'addTimberContext'], 11, 1);
    }

}
