<?php
 
 /**
  * Handles the POPUP after plugin activation
  *
  * @package StockSyncWithGoogleSheetForWooCommerce
  * @since 1.3.2
  */
 
 // Namespace.
 namespace StockSyncWithGoogleSheetForWooCommerce;
 
 // Exit if accessed directly.
 defined('ABSPATH') || exit;
 if ( ! class_exists('\StockSyncWithGoogleSheetForWooCommerce\WCFM') ) {
     /**
      * Add Extra menu for vendor chat
      *
      * @package EMV
      */
     class WCFM {
 		/**
 		 * Constructor - Initializes the class and adds filters and actions.
 		 *
 		 * @return void
 		 */
         public function __construct(){
             add_filter('wcfm_menus', [ $this, 'ws_get_wcfm_menus' ], 30, 1 );
             add_filter( 'wcfm_query_vars', [ $this, 'wcfmcsm_query_vars' ], 50 );
             add_filter( 'wcfm_endpoint_title', [ $this, 'wcfmcsm_endpoint_title' ], 50, 2 );
             add_filter( 'wcfm_endpoints_slug', [ $this, 'wcfm_custom_menus_endpoints_slug' ] );
             add_action( 'wcfm_load_views', [ $this, 'wcfm_csm_load_views' ], 50 );
             add_action( 'before_wcfm_load_views', [ $this, 'wcfm_csm_load_views' ], 50 ); 
         }
         /**
          * Loads custom views for WCFM menus.
          *
          * @param string $end_point The endpoint to load views for.
          */
         public function wcfm_csm_load_views( $end_point ) {
             global $WCFM, $WCFMu;
             $plugin_path = trailingslashit( dirname( __FILE__  ) );
             switch( $end_point ) {
                 case 'flexstock':
                     require_once( $plugin_path . 'views/view.php' );
                 break;
             }
         }
 
         /**
          * Modifies the endpoints slug for custom WCFM menus.
          *
          * @param array $endpoints The existing endpoints.
          * @return array The modified endpoints.
          */
         function wcfm_custom_menus_endpoints_slug( $endpoints ) {
             
             $custom_menus_endpoints = [
                                     'flexstock' => 'flexstock',
 
             ];
             
             $endpoints = array_merge( $endpoints, $custom_menus_endpoints );
             
             return $endpoints;
         }
 
         /**
          * Sets the title for custom WCFM menu endpoints.
          *
          * @param string $title The current title.
          * @param string $endpoint The endpoint being processed.
          * @return string The modified title.
          */
         function wcfmcsm_endpoint_title( $title, $endpoint ) {
             global $wp;
             $title_text = get_option('ssgsw_vendor_menu_name','Sync GSheet"');
             switch ( $endpoint ) {
                 case 'flexstock' :
                     $title = $title_text;
                 break;
             }
             
             return $title;
         }
 
         /**
          * Adds custom query vars for WCFM menus.
          *
          * @param array $query_vars The existing query vars.
          * @return array The modified query vars.
          */
         function wcfmcsm_query_vars( $query_vars ) {
             $wcfm_modified_endpoints = (array) get_option( 'wcfm_endpoints' );  
             $query_custom_menus_vars = array(
                 'flexstock'              => ! empty( $wcfm_modified_endpoints['flexstock'] ) ? $wcfm_modified_endpoints['flexstock'] : 'flexstock',
             );
             $query_vars = array_merge( $query_vars, $query_custom_menus_vars );
             return $query_vars;
         }
 
         /**
          * Registers an extra menu in WCFM for vendor chatting list.
          *
          * @param array $wcfm_menus The existing WCFM menus.
          * @return array The modified WCFM menus.
          * @version 1.0.0
          */
         public function ws_get_wcfm_menus( $wcfm_menus ) {
            // print_r($wcfm_menus);
            $title = get_option('ssgsw_vendor_menu_name','Sync GSheet');
             if ( flexstock_can_vendor() ) {
                 $custom_menu = $wcfm_menus['flexstock'] = [
                     'label' => $title,
                     'url' => $this->get_wcfm_storepep_url( 'flexstock' ),
                     'icon' => 'custom-flexstock-icon', 
                     'class' => 'ssgsw_vendor_menu',
                     'priority' => 6
                 ];
                 $wcfm_menus = array_merge( $wcfm_menus, $custom_menu );
             }
             
             return $wcfm_menus;
         }
 
         /**
          * Generates the URL for a WCFM store endpoint.
          *
          * @param string $endpoint The endpoint to generate the URL for.
          * @return string The generated URL.
          */
         public function get_wcfm_storepep_url($endpoint) {
             global $WCFM;
             $wcfm_page = get_wcfm_page();
             $get_wcfm_settings_url = wcfm_get_endpoint_url( $endpoint, '', $wcfm_page );
             return $get_wcfm_settings_url;
         }    
     }
     new WCFM();
 }