<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Admin_Menu
{
    public const PARENT_SLUG = 'puffergo';

    public static function reorder_menu($menu_order)
    {
        $puffergo_item = 'puffergo';
        $cpt_items = array('edit.php?post_type=puffergo_product', 'edit.php?post_type=puffergo_solution', 'edit.php?post_type=puffergo_case', 'edit.php?post_type=puffergo_testimonial', );
        $present = array_values(array_filter($cpt_items, function ($item) use ($menu_order) { return in_array($item, $menu_order, true); }));

        if (empty($present)) {
            return $menu_order;
        } $menu_order = array_values(array_filter($menu_order, function ($item) use ($present) { return ! in_array($item, $present, true); }));
        $puffergo_pos = array_search($puffergo_item, $menu_order, true);

        if (false !== $puffergo_pos) {
            array_splice($menu_order, $puffergo_pos + 1, 0, $present);
        }

return $menu_order;
    }

    public static function register_menus()
    {
        add_filter('custom_menu_order', '__return_true');
        add_filter('menu_order', array(__CLASS__, 'reorder_menu'));
        $menu_brand = __('PufferGo', 'puffergo');
        add_menu_page($menu_brand, $menu_brand, 'edit_posts', self::PARENT_SLUG, array('PufferGo_Dashboard', 'render_page'), 'dashicons-admin-site-alt3', 3);
        $dashboard_page_title = __('Dashboard', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $dashboard_page_title, $dashboard_page_title, 'edit_posts', self::PARENT_SLUG, array('PufferGo_Dashboard', 'render_page'));
        $site_profile = __('Site Profile', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $site_profile, $site_profile, 'manage_options', 'puffergo-site-profile', array('PufferGo_Site_Profile', 'render_page'));
        $ai_builder = __('AI Builder', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $ai_builder, $ai_builder, 'edit_posts', 'puffergo-ai-site-builder', array('PufferGo_AI_Site_Builder', 'render_admin_page'));
        $inquiries = __('Inquiries', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $inquiries, $inquiries, 'manage_options', 'puffergo-inquiries', array('PufferGo_Contact_Form_Handler', 'render_inquiries_page'));
        $widgets = __('Widgets', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $widgets, $widgets, 'manage_options', 'puffergo-widgets', array('PufferGo_Widgets_Admin', 'render_page'));
        $languages = __('Languages', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $languages, $languages, 'manage_options', PufferGo_Languages_Admin::PAGE_SLUG, array('PufferGo_Languages_Admin', 'render_page'));
        $settings = __('Settings', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $settings, $settings, 'manage_options', 'puffergo-settings', array('PufferGo_Settings', 'render_page'));
        $templates = __('Templates', 'puffergo');
        add_submenu_page(self::PARENT_SLUG, $templates, $templates, 'manage_options', 'puffergo-templates', array('PufferGo_Template_Manager', 'render_page'));

        if (class_exists('PufferGo_Product_Settings', false)) {
            $product_settings = __('Product Settings', 'puffergo');
            add_submenu_page('edit.php?post_type=puffergo_product', $product_settings, $product_settings, 'manage_options', PufferGo_Product_Settings::PAGE_SLUG, array('PufferGo_Product_Settings', 'render_page'));
        }

if (class_exists('PufferGo_Pattern_Playground', false)) {
            $pattern_playground = __('Pattern Playground', 'puffergo');
            add_submenu_page(self::PARENT_SLUG, $pattern_playground, $pattern_playground, 'edit_posts', 'puffergo-pattern-playground', array('PufferGo_Pattern_Playground', 'render_page'));
        }
    }
}
