<?php

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

namespace Kubelio\WooCommerce;

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

/**
 * Setup Class
 */
class Setup {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('after_setup_theme', array($this, 'woocommerce_support'));
		add_filter('body_class', array($this, 'body_class'));

		// Remove/Modify Default WC Elements.
		add_filter('woocommerce_show_page_title', '__return_null');

		add_action('woocommerce_before_main_content', array($this, 'wrapper_start'), 10);
		add_action('woocommerce_after_main_content', array($this, 'wrapper_end'), 10);

		remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
		remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
		remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10);

		add_action('widgets_init', array($this, 'register_sidebars'));
	}

	/**
	 * WooCommerce support
	 */
	public function woocommerce_support() {
		add_theme_support('woocommerce');
	}

	/**
	 * Added Custom Class CSS to Body HTML
	 *
	 * @param array $classes Body classes.
	 * @return array
	 */
	public function body_class($classes) {
		// Layout sidebar on product archive.
		if (kubelio_is_woocommerce('shop')) {
			$classes[]   = 'is-shop-layout-' . get_theme_mod('shop_layout', 'wide');
		}

		// Single product layout body class.
		if (kubelio_is_woocommerce('product')) {
			$classes[] = 'is-product-layout-' . get_theme_mod('product_layout', 'compact');
		}

		// Update cart quantity.
		if (kubelio_is_woocommerce('cart')) {
			$classes[] = 'has-cart-quantity-auto-update';
		}

		// Distraction-Free Checkout.
		if (kubelio_is_woocommerce(array('cart', 'checkout', 'thank_page')) && get_theme_mod('checkout_focus', true)) {
			$classes[] = 'is-checkout-template-focus';
		}

		return $classes;
	}

	/**
	 * Wrapper Start
	 */
	public function wrapper_start() {
		if (! kubelio_is_woocommerce('product')) {
			echo kubelio_get_page_title(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
		echo '<section class="site-wrapper has-global-padding">';
		echo '<div class="site-container is-layout-flow">';

		if (kubelio_is_woocommerce('shop') && in_array(get_theme_mod('shop_layout', 'wide'), array('sidebar', 'sidebar-stack'), true)) {
			echo '<div class="wp-block-columns is-layout-flex is-stacked-on-tablet md:gap-60">';
			echo '<div class="site-content wp-block-column is-layout-flow">';
		}
	}

	/**
	 * Wrapper End
	 */
	public function wrapper_end() {
		if (kubelio_is_woocommerce('shop') && in_array(get_theme_mod('shop_layout', 'wide'), array('sidebar', 'sidebar-stack'), true)) {
			echo '</div>'; // end content.
			echo '<div class="site-sidebar wp-block-column is-layout-flow">';
			get_sidebar('shop');
			echo '</div>';
			echo '</div>'; // end columns.
		}
		echo '</div>';
		echo '</section>';
	}

	/**
	 * Register sidebars
	 */
	public function register_sidebars() {
		register_sidebar(
			array(
				'name'          => esc_html__('Filters Shop', 'kubelio'),
				'id'            => 'kubelio_shop_filter',
				'before_widget' => '<div id="%1$s" class="rt-widget is-widget-aside is-widget-shop-filter %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="rt-widget__heading mb-40"><h3 class="rt-widget__title has-large-font-size">',
				'after_title'   => '</h3></div>',
			)
		);
		register_sidebar(
			array(
				'name'          => esc_html__('Sidebar Shop', 'kubelio'),
				'id'            => 'kubelio_shop_sidebar',
				'before_widget' => '<div id="%1$s" class="rt-widget is-widget-aside is-widget-shop-aside %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="rt-widget__heading mb-40"><h3 class="rt-widget__title has-large-font-size">',
				'after_title'   => '</h3></div>',
			)
		);
	}
}
