<?php

class HTMega_Menu_Term_Meta{

    /**
     * Singleton
     */
    private static $_instance = null;
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Constructor
     */
    private function __construct(){

        add_filter( 'wp_setup_nav_menu_item', [ $this, 'custom_nav_fields_data' ] );
        add_filter('wp_nav_menu_args', [ $this, '_change_nav_menu_args' ], PHP_INT_MAX);

        // Elementor only generates atomic-widget CSS for the main queried post; register
        // mega-menu templates before that one-shot pass so their atomic styles aren't skipped.
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_atomic_styles_for_menu_templates' ], 15 );

    }

    /**
     * Elementor template IDs assigned to items of any currently-enabled mega menu.
     */
    private function get_active_menu_template_ids() {
        $template_ids = [];

        $locations = get_nav_menu_locations();
        if ( empty( $locations ) ) {
            return $template_ids;
        }

        foreach ( $locations as $menu_id ) {
            if ( ! $menu_id ) {
                continue;
            }

            $settings = get_option( 'ht_menu_options_' . $menu_id );
            if ( empty( $settings['enable_menu'] ) || 'on' !== $settings['enable_menu'] ) {
                continue;
            }

            $items = wp_get_nav_menu_items( $menu_id );
            if ( empty( $items ) ) {
                continue;
            }

            foreach ( $items as $item ) {
                if ( ! empty( $item->template ) ) {
                    $template_ids[] = absint( $item->template );
                }
            }
        }

        return array_unique( array_filter( $template_ids ) );
    }

    /**
     * Register mega-menu Elementor templates with Elementor's atomic-widget CSS pipeline
     * before its own single per-request enqueue pass runs (Frontend::enqueue_styles(),
     * hooked at wp_enqueue_scripts priority 20). That pass fires
     * 'elementor/frontend/after_enqueue_post_styles' unconditionally on every request, which
     * is what actually triggers the atomic CSS generation for every post id registered via
     * 'elementor/post/render' so far — so we only need to register our template ids here
     * (priority 15, before Elementor's own priority 20 pass), not force-run enqueue_styles()
     * ourselves. Calling enqueue_styles() directly would trip its internal static one-shot
     * guard and race with any other extension (e.g. the header/footer builder) doing the same,
     * silently dropping whichever extension's ids weren't registered yet at that moment.
     */
    public function enqueue_atomic_styles_for_menu_templates() {
        if ( is_admin() || ! class_exists( '\Elementor\Plugin' ) ) {
            return;
        }

        $template_ids = $this->get_active_menu_template_ids();
        if ( empty( $template_ids ) ) {
            return;
        }

        foreach ( $template_ids as $template_id ) {
            do_action( 'elementor/post/render', $template_id );
        }
    }

    public function custom_nav_fields_data( $menu_item ){

        // Get Menu Item Custom Data
        $item_settings = get_post_meta( $menu_item->ID, 'htmega_menu_settings', true );

        
          // Settings
        if( !empty( $item_settings['menu-item-menuwidth-'.$menu_item->ID] ) ){
            $menu_item->menuwidth = $item_settings['menu-item-menuwidth-'.$menu_item->ID];
        }
        
        if( !empty( $item_settings['menu-item-menuposition-'.$menu_item->ID] ) ){
            $menu_item->menuposition = $item_settings['menu-item-menuposition-'.$menu_item->ID];
        }
        if( !empty( $item_settings['menu-item-template-'.$menu_item->ID] ) ){
            $menu_item->template = $item_settings['menu-item-template-'.$menu_item->ID];
        }
        // Icon
        if( !empty( $item_settings['menu-item-ficon-'.$menu_item->ID] ) ){
            $menu_item->ficon = $item_settings['menu-item-ficon-'.$menu_item->ID ];
        }
        if( !empty( $item_settings['menu-item-ficoncolor-'.$menu_item->ID] ) ){
            $menu_item->ficoncolor = $item_settings['menu-item-ficoncolor-'.$menu_item->ID];
        }

        // Badges
        if( !empty( $item_settings['menu-item-menutag-'.$menu_item->ID] ) ){
            $menu_item->menutag = $item_settings['menu-item-menutag-'.$menu_item->ID];
        }
        if( !empty( $item_settings['menu-item-menutagcolor-'.$menu_item->ID] ) ){
            $menu_item->menutagcolor = $item_settings['menu-item-menutagcolor-'.$menu_item->ID];
        }

        if( !empty( $item_settings['menu-item-badge-color-type-'.$menu_item->ID] ) ){
            $menu_item->badge_bg_type = $item_settings['menu-item-badge-color-type-'.$menu_item->ID];
        }
        if( !empty( $item_settings['menu-item-menutagbgcolor-'.$menu_item->ID] ) ){
            $menu_item->menutagbgcolor = $item_settings['menu-item-menutagbgcolor-'.$menu_item->ID];
        }
        if( !empty( $item_settings['menu-item-badge-bg-two-'.$menu_item->ID] ) ){
            $menu_item->badge_bg_color_two = $item_settings['menu-item-badge-bg-two-'.$menu_item->ID];
        }


        return $menu_item;
    }

    function _change_nav_menu_args( $args ){

        if ( is_admin() ) {
            return $args;
        }

        $current_theme_location = isset( $args['theme_location'] ) ? $args['theme_location'] : '';

        $locations = get_nav_menu_locations();

        // Menu ID
        if ( ! isset( $locations[ $current_theme_location ] ) ) return $args;     
        $menu_id = isset( $locations[ $current_theme_location ] ) ? $locations[ $current_theme_location ] : '';
        if ( ! $menu_id ) return $args;

        // Menu And Location
        if (!empty($locations[$args['theme_location']])) {
            $menu = wp_get_nav_menu_object($locations[$args['theme_location']]);
        } elseif (!empty($args['menu'])) {
            $menu = wp_get_nav_menu_object($args['menu']);
        } else {
            $menus = (array)wp_get_nav_menus();
            if ($menus) {
                foreach ($menus as $menu) {
                    $has_items = wp_get_nav_menu_items($menu->term_id, ['update_post_term_cache' => false]);
                    if ($has_items) break;
                }
            }
        }

        if (!isset($menu) || is_wp_error($menu) || !is_object($menu)) {
            return $args;
        }

        $settings = get_option( "ht_menu_options_" . $menu_id );
        if ( isset ( $settings['enable_menu'] ) && $settings['enable_menu'] == 'on' ) {

            $htmega_on_mobile = '<a href="#" class="htmobile-aside-button"><i class="fa fa-bars"></i></a>';
            $htmega_on_mobile_menu = '<div class="htmobile-menu-wrap"><a class="htmobile-aside-close"><i class="fa fa-times"></i></a><div class="htmobile-navigation"><ul id="%1$s" class="%2$s">%3$s</ul></div></div>';

            $items_wrap = '<div class="htmega-menu-area"><ul id="%1$s" class="%2$s">%3$s</ul>'.$htmega_on_mobile.'</div>'.$htmega_on_mobile_menu;

            $args = [
                'menu'            => $args['menu'],
                'menu_id'         => $args['menu_id'],
                'menu_class'      => 'htmega-megamenu',
                'theme_location'  => $args['theme_location'],
                'container'       => 'div',
                'container_id'    => $args['container_id'],
                'container_class' => 'htmega-menu-container',
                'fallback_cb'     => false,
                'before'          => $args['before'],
                'after'           => $args['after'],
                'link_before'     => $args['link_before'],
                'link_after'      => $args['link_after'],
                'echo'            => $args['echo'],
                'depth'           => $args['depth'],
                'items_wrap'      => $items_wrap,
                'item_spacing'    => isset($args['item_spacing']) ? $args['item_spacing'] : '',
                'walker'          => new HTMega_Menu_Nav_Walker()
            ];
            return apply_filters( 'htmegamenu_nav_menu_args', $args );

        }else{ return $args; }

    }
    public function get_menu_id( $location = null ) {
        $locations = get_nav_menu_locations();
        return isset( $locations[ $location ] ) ? $locations[ $location ] : false;
    }

}
HTMega_Menu_Term_Meta::instance();