<?php

namespace Dispatch\Config;

use Timber\Menu as TimberMenu;

class Menus {

    protected $menus;

    public function __construct() {

        $this->menus = array(
            'main_menu' => __('Main Menu'),
            'footer_menu' => __('Footer Menu'),
            'construction_menu' => __('Construction top Menu'),
            'explore_menu' => __('Explore top Menu'),
            'about_menu' => __('About top Menu'),
            'home_menu' => __('Home top Menu'),
            'environments_menu' => __('Environments top Menu'),

        );

        add_filter( 'init', [$this, 'register'] );
        add_filter( 'dispatch_add_to_context', [$this, 'add_to_context'] );
        add_filter( 'nav_menu_css_class', [$this, 'custom_active_item_classes'], 10, 2 );

    }

    /**
     * register menus with wordpress
     */
    public function register() {

        register_nav_menus( $this->menus );

    }

    /**
     * add menus to timber context
     * consider the required scope before adding menus to the global context
     */
    public function add_to_context( $context ) {

        foreach( $this->menus as $key => $name ) {
            $context[$key] = new TimberMenu( $key );
        }

        return $context;

    }

    public function custom_active_item_classes($classes = array(), $menu_item = false){
        global $post;
        if (isset($post->post_type)) {
            $classes[] = ($menu_item->url == get_post_type_archive_link($post->post_type)) ? 'current-menu-item active' : '';
        }
        return $classes;
    }

}
