<?php
/**
 * LiveLang Menu Metabox Class
 *
 * Adds a custom metabox to the WordPress Menu editor to allow users to add the language switcher to their menus.
 *
 * @package LiveLang
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class LiveLang_Menu_Metabox {

    /**
     * Instance of this class.
     *
     * @var LiveLang_Menu_Metabox
     */
    private static $instance = null;

    /**
     * Get instance of this class.
     *
     * @return LiveLang_Menu_Metabox
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor.
     */
    public function __construct() {
        add_action( 'admin_init', array( $this, 'add_language_switcher_metabox' ) );
    }

    /**
     * Add the metabox to the Appearance > Menus page.
     */
    public function add_language_switcher_metabox() {
        add_meta_box(
            'livelang-custom-switcher',
            __( 'Language Switcher', 'livelang' ),
            array( $this, 'render_language_switcher_metabox' ),
            'nav-menus',
            'side',
            'default'
        );
    }

    /**
     * Render the metabox content.
     */
    public function render_language_switcher_metabox() {
        global $_nav_menu_placeholder, $nav_menu_selected_id;
        
        // Follow Polylang's way of generating placeholder ID to avoid conflicts.
        $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? $_nav_menu_placeholder - 1 : -1;
        ?>
        <div id="livelang-switcher" class="posttypediv">
            <div id="tabs-panel-livelang-switcher" class="tabs-panel tabs-panel-active">
                <ul id="livelang-switcher-checklist" class="categorychecklist form-no-clear">
                    <li>
                        <label class="menu-item-title">
                            <input type="checkbox" class="menu-item-checkbox"
                                   name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-object-id]" value="-1">
                            <?php esc_html_e( 'Language Switcher', 'livelang' ); ?>
                        </label>

                        <input type="hidden" class="menu-item-type" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-type]" value="custom">
                        <input type="hidden" class="menu-item-title" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-title]" value="Language Switcher">
                        <input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-url]" value="#livelang_switcher">
                        <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo (int) $_nav_menu_placeholder; ?>][menu-item-classes]" value="menu-item-language-switcher">
                    </li>
                </ul>
            </div>
            <p class="button-controls">
                <span class="add-to-menu">
                    <input type="submit" <?php disabled( $nav_menu_selected_id, 0 ); ?> 
                           class="button-secondary submit-add-to-menu right" 
                           value="<?php esc_attr_e( 'Add to Menu', 'livelang' ); ?>" 
                           name="add-post-type-menu-item" id="submit-livelang-switcher">
                    <span class="spinner"></span>
                </span>
            </p>
        </div>
        <?php
    }
}

// Initialize the class.
LiveLang_Menu_Metabox::instance();