<?php
/**
 * WooCommerce Header Cart Header Customizer Options
 *
 * @package Zenvy
 */

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

if ( ! class_exists( 'Zenvy_WooCommerce_Cart_Header' ) ) :

	/**
	 * WooCommerce Cart Header Builder class.
	 */
	class Zenvy_WooCommerce_Cart_Header {

		/**
		 * Panel ID
		 *
		 * @var string
		 * @access public
		 * @since 1.0.0
		 */
		public $element = 'wc_cart';

		/**
		 * Main Instance
		 *
		 * Insures that only one instance of Zenvy_WooCommerce_Cart_Header exists in memory at any one
		 * time. Also prevents needing to define globals all over the place.
		 *
		 * @since  1.0.0
		 * @access public
		 *
		 * @return object
		 */
		public static function instance() {

			// Store the instance locally to avoid private static replication.
			static $instance = null;

			// Only run these methods if they haven't been ran previously.
			if ( null === $instance ) {
				$instance = new Zenvy_WooCommerce_Cart_Header();
			}

			// Always return the instance.
			return $instance;
		}

		/**
		 * Run functionality with hooks.
		 *
		 * @since  1.0.0
		 * @access public
		 *
		 * @return void
		 */
		public function run() {

			add_filter( 'zenvy_customizer_header_builder_items', [ $this, 'add_zenvy_header_builder_item' ] );
			add_action( 'customize_register', [ $this, 'customize_register' ], 3 );
			add_filter( 'zenvy_get_template_part', [ $this, 'get_template_part' ], 10, 2 );
		}

		/**
		 * Add Item on Header Builder.
		 *
		 * @since  1.0.0
		 *
		 * @param array $zenvy_header_builder_item Existing header builder items array.
		 * @return array
		 */
		public function add_zenvy_header_builder_item( $zenvy_header_builder_item ) {
			$zenvy_header_builder_item[ $this->element ] = [
				'icon'    => 'dashicons dashicons-cart',
				'name'    => esc_html__( 'Cart', 'zenvy' ),
				'id'      => $this->element,
				'section' => $this->element,
			];

			return $zenvy_header_builder_item;
		}

		/**
		 * Callback functions for customize_register.
		 *
		 * @since  1.0.2
		 * @access public
		 *
		 * @param WP_Customize_Manager $wp_customize WP Customizer Manager instance.
		 * @return void
		 */
		public function customize_register( $wp_customize ) {

			$wp_customize->add_section(
				zenvy_woocommerce_cart_header()->element,
				[
					'title'    => esc_html__( 'WC Cart', 'zenvy' ),
					'priority' => 80,
					'panel'    => Zenvy_Customizer_Header_Builder()->panel,
				]
			);
			require ZENVY_THEME_DIR . 'inc/customizer/builder/header/options/woocommerce/cart/Zenvy_Customize_Header_WooCommerce_Cart_Fields.php';
		}

		/**
		 * Load template part.
		 *
		 * @since  1.0.0
		 *
		 * @param string $template Template file path.
		 * @param string $id       Template ID slug.
		 * @return string
		 */
		public function get_template_part( $template, $id ) {
			if ( ! $template && file_exists( ZENVY_THEME_DIR . "/template-parts/header/woocommerce/{$id}.php" ) ) {
				$template = ZENVY_THEME_DIR . "/template-parts/header/woocommerce/{$id}.php";
			}
			return $template;
		}
	}
endif;

if ( ! function_exists( 'zenvy_woocommerce_cart_header' ) ) {

	/**
	 * Create Instance for Zenvy_WooCommerce_Cart_Header.
	 *
	 * @since  1.0.0
	 * @access public
	 *
	 * @return object
	 */
	function zenvy_woocommerce_cart_header() {
		return Zenvy_WooCommerce_Cart_Header::instance();
	}

	if ( Zenvy_Helper::is_woocommerce() ) {
		zenvy_woocommerce_cart_header()->run();
	}
}
