<?php

/**
 * WooCommerce Scripts Class.
 *
 * @package Kubelio\WooCommerce
 * @since 1.0.0
 */

namespace Kubelio\WooCommerce;

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

/**
 * Scripts Class
 */
class Scripts {

	private $theme_version;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->theme_version = KUBELIO_THEME_VERSION;

		add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'), 90);
		add_action('after_setup_theme', array($this, 'editor_style'), 98);
	}

	/**
	 * Enqueue WooCommerce scripts and styles.
	 */
	public function enqueue_scripts() {
		// Global styles
		wp_enqueue_style("kubelio-woocommerce", get_template_directory_uri() . '/assets/css/plugins/woocommerce.min.css', array('woocommerce-general'), $this->theme_version);
		wp_enqueue_style("kubelio-woocommerce-block", get_template_directory_uri() . '/assets/css/plugins/woocommerce-block.min.css', array('woocommerce-general'), $this->theme_version);
		wp_enqueue_style("kubelio-woocommerce-mini-cart", get_template_directory_uri() . '/assets/css/plugins/woocommerce-mini-cart.min.css', array('woocommerce-general'), $this->theme_version);

		// Quantity increment field
		wp_enqueue_script("kubelio-quantity-field", get_template_directory_uri() . '/assets/js/quantity-field.min.js', array(), $this->theme_version, true);

		// Shop specific
		if (kubelio_is_woocommerce('shop')) {
			wp_enqueue_script("kubelio-shop", get_template_directory_uri() . '/assets/js/shop.min.js', array(), $this->theme_version, true);
		}

		// Cart specific
		if (kubelio_is_woocommerce('cart')) {
			wp_enqueue_script("kubelio-cart", get_template_directory_uri() . '/assets/js/cart.min.js', array('jquery', "kubelio-swiper"), $this->theme_version, true);
		}

		// Mini cart
		wp_enqueue_script("wc-cart-fragments");

		// Mini-cart quantity AJAX updater (classic widget)
		wp_enqueue_script("kubelio-mini-cart", get_template_directory_uri() . '/assets/js/mini-cart.min.js', array('jquery'), $this->theme_version, true);

		// Checkout specific
		if (kubelio_is_woocommerce('checkout')) {
			wp_enqueue_script("kubelio-checkout-move-coupon", get_template_directory_uri() . '/assets/js/checkout-move-coupon.min.js', array(), $this->theme_version, true);
		}

		// Single product specific (Gallery)
		if (kubelio_is_woocommerce('product')) {
			// GLightbox
			wp_enqueue_style('glightbox', get_template_directory_uri() . '/assets/glightbox/glightbox.min.css', array(), $this->theme_version);
			wp_enqueue_script('glightbox', get_template_directory_uri() . '/assets/glightbox/glightbox.min.js', array(), $this->theme_version, true);

			// Product gallery
			wp_enqueue_script("kubelio-product-gallery", get_template_directory_uri() . '/assets/js/product-gallery.min.js', array('jquery', 'swiper', 'glightbox'), $this->theme_version, true);
		}
	}

	/**
	 * WooCommerce block editor style
	 */
	public function editor_style() {
		add_editor_style('assets/css/plugins/woocommerce.min.css');
		add_editor_style('assets/css/plugins/woocommerce-block.min.css');
	}
}
