<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function mobile_api_enqueue_assets() {
	wp_enqueue_style( 'mobile-api-style', plugins_url( '../assets/css/style.css', __FILE__ ), array(), mobile_api_get_version_number() );
	wp_enqueue_script( 'mobile-api-script', plugins_url( '../assets/js/script.js', __FILE__ ), array( 'jquery' ), mobile_api_get_version_number(), true );

	if ( is_single() && get_post_type() == 'mobile-api-vehicle' ) :
		wp_enqueue_style( 'slick-style', plugins_url( '../assets/js/slick/slick.min.css', __FILE__ ), array(), mobile_api_get_version_number() );
		wp_enqueue_style( 'slick-theme-style', plugins_url( '../assets/js/slick/slick-theme.css', __FILE__ ), array(), mobile_api_get_version_number() );
		wp_enqueue_script( 'slick-script', plugins_url( '../assets/js/slick/slick.min.js', __FILE__ ), array( 'jquery' ), mobile_api_get_version_number(), true );
		wp_enqueue_script( 'mobile-api-single', plugins_url( '../assets/js/single.js', __FILE__ ), array( 'jquery' ), mobile_api_get_version_number(), true );
	endif;

	if ( is_page() ) :
		global $post;
		$content = $post->post_content;
		if ( !empty( $content ) && has_shortcode( $content, 'mobile-api-vehicle-archive' ) || ( class_exists( '\Elementor\Plugin' ) && str_contains( $content, 'Mobile_API_Elementor_Vehicle_Archive' ) ) )
			wp_enqueue_style( 'mobile-api-vehicle-archive-style', plugins_url( '../block/vehicle-archive/style.css', __FILE__ ), array(), mobile_api_get_version_number() );
    endif;
}
add_action( 'wp_enqueue_scripts', 'mobile_api_enqueue_assets', 15 );

function mobile_api_enqueue_assets_admin() {
	wp_enqueue_style( 'mobile-api-admin-style', plugins_url( '../assets/css/admin.css', __FILE__ ), array(), mobile_api_get_version_number() );
	// wp_enqueue_script( 'mobile-api-admin-script', plugins_url( '../assets/js/admin.js', __FILE__ ), array( 'jquery' ), mobile_api_get_version_number(), true );
}
add_action( 'admin_enqueue_scripts', 'mobile_api_enqueue_assets_admin', 20 );

function mobile_api_enqueue_assets_ajax() {
	wp_enqueue_script( 'mobile-api-ajax', plugins_url( '../assets/js/ajax.js', __FILE__ ), array( 'jquery' ), mobile_api_get_version_number(), true );

	wp_localize_script( 'mobile-api-ajax', 'mobile_api_ajax', array(
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
		'nonce'   => wp_create_nonce( 'mobile_api-ajax-nonce' )
		)
	);
}
add_action( 'wp_enqueue_scripts', 'mobile_api_enqueue_assets_ajax', 25 );
add_action( 'admin_enqueue_scripts', 'mobile_api_enqueue_assets_ajax', 25 );