<?php
/**
 *
 * @package     productive-commerce
 * @author      productiveminds.com
 * @copyright   productiveminds.com
 */

if ( !defined('ABSPATH') ) {
	die();
}

if ( !function_exists( 'productive_elementor_add_widget_category' ) ) {
    /**
     * Register a new Elementor Category for listing ProductiveMinds' Elementor Widgets
     * @param type $elements_manager
     */
    function productive_elementor_add_widget_category( $elements_manager ) {
        $elements_manager->add_category(
            'productiveminds',
            [
                    'title' => esc_html__( 'ProductiveMinds', 'productive-commerce' ),
                    'icon' => 'fa fa-heart',
            ]
        );
    }
    add_action( 'elementor/elements/categories_registered', 'productive_elementor_add_widget_category' );
}

if ( !function_exists( 'productive_elementor_elementor_dynamic_tag_group' ) ) {
    // Start:: Dynamic Tags
    /**
     * Dynamic Tags Groups
     * @param type $dynamic_tags_manager
     */
    function productive_elementor_elementor_dynamic_tag_group( $dynamic_tags_manager ) {
        $dynamic_tags_manager->register_group(
            'productive_dynamic_tags',
            [
                'title' => esc_html__( 'Productive Dynamic Tags', 'productive-commerce' )
            ]
        );
    }
    add_action( 'elementor/dynamic_tags/register', 'productive_elementor_elementor_dynamic_tag_group' );
}

/**
 * Widgets
 * @param type $widgets_manager
 */
function productive_commerce_elementor_register_widgets( $widgets_manager ) {
    if( productive_global_is_pass_addons_active() ) {
        if( class_exists( 'Pass_Addons_Common_Settings' ) && pass_addons_performance_is_active_page_builder_elementor() ) {
            // Compare
            if ( 'checked' == Pass_Addons_Common_Settings::pass_addons_get_a_elementor_widget_status( 'compare_list', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/compare/productive-elementor-widget-compare-list.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Compare_List() );
            }
            // Wishlist
            if ( 'checked' == Pass_Addons_Common_Settings::pass_addons_get_a_elementor_widget_status( 'wishlist_list', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/wishlist/productive-elementor-widget-wishlist-list.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Wishlist_List() );
            }
        }
    } else if( productive_global_is_ravolat_addons_active() ) {
        if( class_exists( 'Ravolat_Addons_Common_Settings' ) && ravolat_addons_performance_is_active_page_builder_elementor() ) {
            // Compare
            if ( 'checked' == Ravolat_Addons_Common_Settings::ravolat_addons_get_a_elementor_widget_status( 'compare_list', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/compare/productive-elementor-widget-compare-list.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Compare_List() );
            }
            // Wishlist
            if ( 'checked' == Ravolat_Addons_Common_Settings::ravolat_addons_get_a_elementor_widget_status( 'wishlist_list', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/wishlist/productive-elementor-widget-wishlist-list.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Wishlist_List() );
            }
        }
    } else {
        // Compare List
        require_once( plugin_dir_path( __FILE__ ) . 'widgets/compare/productive-elementor-widget-compare-list.php' );
        $widgets_manager->register( new \Productive_Elementor_Widget_Compare_List() );
        // Wishlist List
        require_once( plugin_dir_path( __FILE__ ) . 'widgets/wishlist/productive-elementor-widget-wishlist-list.php' );
        $widgets_manager->register( new \Productive_Elementor_Widget_Wishlist_List() );
    }
    
}
add_action( 'elementor/widgets/register', 'productive_commerce_elementor_register_widgets' );


/**
 * Use hook to register JS
 */
function productive_commerce_elementor_register_widget_dependencies() {
    global $productive_commerce_plugin_version;
    $productiveminds_global_localize_script_vars = array();
    wp_register_script( 'productiveminds_lib_elementor_productive_commerce_js_handle', PRODUCTIVE_COMMERCE_PLUGIN_URI . 'standard/elementor/assets/js/productiveminds-library-support-elementor-js.min.js', array( 'elementor-frontend' ), $productive_commerce_plugin_version, true );
    
    productive_global_get_common_swiper_localize_script( $productiveminds_global_localize_script_vars );
    
    wp_localize_script(
        'productiveminds_lib_elementor_productive_commerce_js_handle',
        'productiveminds_lib_elementor_productive_commerce_js_name',
        $productiveminds_global_localize_script_vars
        );
}
add_action( 'wp_enqueue_scripts', 'productive_commerce_elementor_register_widget_dependencies', 85 );
