<?php
/**
 * Jewelry Market functions and definitions
 *
 * @package jewelry-market
 */

if ( ! function_exists( 'jewelry_market_support' ) ) :

	function jewelry_market_support() {

		load_theme_textdomain( 'jewelry-market', get_template_directory() . '/languages' );

		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'align-wide' );
		add_theme_support( 'responsive-embeds' );
		add_theme_support( 'experimental-link-color' );

		// Editor style
		add_editor_style( 'style.css' );
	}

endif;
add_action( 'after_setup_theme', 'jewelry_market_support' );

/**
 * Enqueue Styles & Scripts
 */
function jewelry_market_assets() {

	$theme_version = wp_get_theme()->get( 'Version' );

	/**
	 * ======================
	 * STYLES
	 * ======================
	 */

	// Main stylesheet
	wp_enqueue_style(
		'jewelry-market-style',
		get_template_directory_uri() . '/style.css',
		array(),
		$theme_version
	);

	wp_style_add_data( 'jewelry-market-style', 'rtl', 'replace' );

	// Dashicons
	wp_enqueue_style( 'dashicons' );

	// Font Awesome
	wp_enqueue_style(
		'fontawesome',
		get_template_directory_uri() . '/inc/fontawesome/css/all.css',
		array(),
		'6.7.0'
	);

	// Animate CSS
	wp_enqueue_style(
		'animate-css',
		get_template_directory_uri() . '/assets/css/animate.css',
		array(),
		$theme_version
	);

	// Owl Carousel
	wp_enqueue_style(
		'owl-carousel-style',
		get_template_directory_uri() . '/assets/css/owl.carousel.css',
		array(),
		$theme_version
	);

	// Swiper CSS
	wp_enqueue_style(
		'swiper-css',
		get_template_directory_uri() . '/assets/css/swiper-bundle.css',
		array(),
		$theme_version
	);

	/**
	 * ======================
	 * SCRIPTS
	 * ======================
	 */

	// WOW JS
	wp_enqueue_script(
		'wow',
		get_template_directory_uri() . '/assets/js/wow.js',
		array('jquery'),
		$theme_version,
		true
	);

	// Custom JS
	wp_enqueue_script(
		'jewelry-market-custom',
		get_template_directory_uri() . '/assets/js/custom.js',
		array('jquery'),
		$theme_version,
		true
	);

	// Scroll to Top
	wp_enqueue_script(
		'jewelry-market-scroll-to-top',
		get_template_directory_uri() . '/assets/js/scroll-to-top.js',
		array(),
		$theme_version,
		true
	);

	// Swiper JS
	wp_enqueue_script(
		'swiper-js',
		get_template_directory_uri() . '/assets/js/swiper-bundle.js',
		array(),
		$theme_version,
		true
	);

	// Owl Carousel JS
	wp_enqueue_script(
		'owl-carousel-js',
		get_template_directory_uri() . '/assets/js/owl.carousel.js',
		array('jquery'),
		$theme_version,
		true
	);
}
add_action( 'wp_enqueue_scripts', 'jewelry_market_assets' );

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {
    if ($hook !== 'appearance_page_jewelry-market') return;

    wp_enqueue_script('admin-notice-script', get_template_directory_uri() . '/get-started/js/admin-notice-script.js', ['jquery'], null, true);
    wp_localize_script('admin-notice-script', 'pluginInstallerData', [
        'ajaxurl'     => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('install_activate_nonce'),
        'redirectUrl' => admin_url('themes.php?page=jewelry-market'),
    ]);
});

add_action('wp_ajax_check_plugin_activation', function () {
    if (!isset($_POST['plugin']) || empty($_POST['plugin'])) {
        wp_send_json_error(['message' => 'Missing plugin identifier']);
    }

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    // Map plugin identifiers to their main files
    $jewelry_market_plugin_map = [
        'woocommerce'          => 'woocommerce/woocommerce.php',
        'wordclever_ai_content_writer'    => 'wordclever-ai-content-writer/wordclever.php',  
		'gutentor' => 'gutentor/gutentor.php',  
    ];

    $jewelry_market_requested_plugin = sanitize_text_field($_POST['plugin']);

    if (!isset($jewelry_market_plugin_map[$jewelry_market_requested_plugin])) {
        wp_send_json_error(['message' => 'Invalid plugin']);
    }

    $jewelry_market_plugin_file = $jewelry_market_plugin_map[$jewelry_market_requested_plugin];
    $jewelry_market_is_active   = is_plugin_active($jewelry_market_plugin_file);

    wp_send_json_success(['active' => $jewelry_market_is_active]);
});

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

function jewelry_market_theme_setting() {
	
// Add block patterns
require get_template_directory() . '/inc/block-pattern.php';

// Add block Style
require get_template_directory() . '/inc/block-style.php';

// TGM
require get_template_directory() . '/inc/tgm/plugin-activation.php';

// Get Started
require get_template_directory() . '/get-started/getstart.php';

// Get Notice
require get_template_directory() . '/get-started/notice.php';

// Demo Import
require get_template_directory() . '/get-started/demo-import.php';

// Get Notice
require get_template_directory() . '/inc/customizer.php';

}
add_action('after_setup_theme', 'jewelry_market_theme_setting');
