<?php


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

/**
 * Products shortcode class.
 */
class ebz_default_theme {


	/**
	 * Attributes.
	 *
	 * @since 3.2.0
	 * @var   array
	 */
	protected $attributes = array();

	/**
	 * Query args.
	 *
	 * @since 3.2.0
	 * @var   array
	 */
	protected $query_args = array();

	/**
	 * Initialize shortcode.
	 *
	 * @since 3.2.0
	 * @param array  $attributes Shortcode attributes.
	 * @param string $type       Shortcode type.
	 */
	public function __construct( $attributes = array(), $zltid = 'notset', $type = 'products' ) {
		$this->type       		= $type;
		$this->attributes 		= $this->parse_attributes( $attributes );
		$this->splide_id  		= uniqid('z');
		$this->block_id   		= uniqid('z');
		$this->a_p_h      		= uniqid('z');
		$this->zltid      		= $zltid;
		$this->grids_wrapper  = uniqid('z');
		$this->t_p_wrapper   	= uniqid('z');
	}

	/**
	 * Get shortcode content.
	 *
	 * @since  3.2.0
	 * @return string
	 */
	public function get_content($products, $rec_settings) {
		return $this->product_loop($products, $rec_settings);
	}

	/**
	 * Get slider content.
	 *
	 * @since  2022.09.23
	 * @return string
	 */
	public function get_default_settings($rec_settings) {

		/*
		 * Set default vaules for needed variables
		 */

		 $default_settings = array(
 				'title' => 'You may also like',
 				'desktop' => array(
 						'products' => 4,
 						'rows' => 1,
 						'slider_settings' => array(
 								'slider' => false,
 								'products_per_slide' => 4,
 								'use_arrows' => false,
 								'arrow_color' => '#1f30cf80',
 								'arrow_background_color' => '#687beb2f',
 								'use_dots' => false,
 								'dots_color' => '#ccccccff',
 								'dots_active_color' => '#abb7e0',
 								'gap' => '16px'
 						),
 						'title_settings' => array(
 								'title' => 'You may also like'
 						)
 				),
 				'mobile' => array(
 						'products' => 2,
 						'rows' => 1,
 						'slider_settings' => array(
 								'slider' => true,
 								'products_per_slide' => 1,
 								'use_arrows' => false,
 								'arrow_color' => '#1f30cf80',
 								'arrow_background_color' => '#687beb2f',
 								'use_dots' => true,
 								'dots_color' => '#ccccccff',
 								'dots_active_color' => '#abb7e0',
 								'gap' => '6px'
						)
 				)
 		);

 		$rec_settings = array_merge($default_settings, $rec_settings);

		return $rec_settings;
	}

	/**
	 * Get slider content.
	 *
	 * @since  2022.09.23
	 * @return string
	 */
	public function get_slider($atts, $rec_settings) {

		/*
		 * Set default vaules for needed variables
		 */

		$rec_settings = $this->get_default_settings($rec_settings);

		$columns = $rec_settings['desktop']['products'];
		if (array_key_exists("columns", $this->attributes) && $this->attributes['columns'] != '') {
			$columns = absint( $this->attributes['columns'] );
		}

		/*
		 * Set vaules for needed variables
		 */

 		$zl_desktop_splide_arrow_color = $rec_settings['desktop']['slider_settings']['arrow_color'];
 		$zl_desktop_splide_dots_color = $rec_settings['desktop']['slider_settings']['dots_color'];
 		$zl_desktop_splide_dots_active_color = $rec_settings['desktop']['slider_settings']['dots_active_color'];
 		$zl_desktop_splide_arrow_background_color = $rec_settings['desktop']['slider_settings']['arrow_background_color'];
 		$zl_desktop_splide_use_arrows = $rec_settings['desktop']['slider_settings']['use_arrows'];
 		$zl_desktop_splide_use_dots = $rec_settings['desktop']['slider_settings']['use_dots'];
		$zl_desktop_use_splide = $rec_settings['desktop']['slider_settings']['slider'];
		$zl_desktop_splide_perPage = $rec_settings['desktop']['slider_settings']['products_per_slide'];

		$zl_desktop_splide_gap = '16px';
		if (isset($rec_settings['desktop']['slider_settings']['gap'])) {
			$zl_desktop_splide_gap = $rec_settings['desktop']['slider_settings']['gap'];
		}

		$zl_mobile_splide_arrow_color = $rec_settings['mobile']['slider_settings']['arrow_color'];
		$zl_mobile_splide_dots_color = $rec_settings['mobile']['slider_settings']['dots_color'];
		$zl_mobile_splide_dots_active_color = $rec_settings['mobile']['slider_settings']['dots_active_color'];
		$zl_mobile_splide_arrow_background_color = $rec_settings['mobile']['slider_settings']['arrow_background_color'];
		$zl_mobile_splide_use_arrows = $rec_settings['mobile']['slider_settings']['use_arrows'];
		$zl_mobile_splide_use_dots = $rec_settings['mobile']['slider_settings']['use_dots'];
		$zl_mobile_use_splide = $rec_settings['mobile']['slider_settings']['slider'];
		$zl_mobile_splide_perPage = $rec_settings['mobile']['slider_settings']['products_per_slide'];

		$zl_mobile_splide_gap = '6px';
		if (isset($rec_settings['mobile']['slider_settings']['gap'])) {
			$zl_mobile_splide_gap = $rec_settings['mobile']['slider_settings']['gap'];
		}

		if (array_key_exists("data-slide", $atts) && (int)$atts['data-slide'] > 0) {
			 $zl_desktop_splide_perPage = (int)$atts['data-slide'];
			 $zl_desktop_splide_use_arrows = true;
	 		 $zl_desktop_use_splide = true;
		} else if (array_key_exists("data-slide", $atts) && (int)$atts['data-slide'] == 0) {
			 $zl_desktop_use_splide = false;
	 	}

		if (array_key_exists("data-mslide", $atts) && (int)$atts['data-mslide'] > 0) {
			$zl_mobile_splide_perPage = (int)$atts['data-mslide'];
			$zl_mobile_use_splide = true;
		} else if (array_key_exists("data-mslide", $atts) && (int)$atts['data-mslide'] == 0) {
			$zl_mobile_use_splide = false;
		}

		$zl_desktop_use_splide = $zl_desktop_use_splide ? 'true' : 'false';
		$zl_mobile_use_splide = $zl_mobile_use_splide ? 'true' : 'false';

		$zl_desktop_splide_use_arrows = $zl_desktop_splide_use_arrows ? 'flex' : 'none';
		$zl_mobile_splide_use_arrows = $zl_mobile_splide_use_arrows ? 'flex' : 'none';

		$zl_desktop_splide_use_dots = $zl_desktop_splide_use_dots ? 'flex' : 'none';
		$zl_mobile_splide_use_dots = $zl_mobile_splide_use_dots ? 'flex' : 'none';

		$use_switch_attributes = 'false';
		$switch_attributes_source = '';
		$switch_attributes_destination = '';
		if (array_key_exists("switch_attributes", $atts) && $atts['switch_attributes'] != '') {
			$switch_attributes = explode('|', $atts['switch_attributes']);
			$switch_attributes_source = array_shift($switch_attributes);
			$switch_attributes_destination = end($switch_attributes);
			$use_switch_attributes = 'true';
		}

		/*
		The following style fixes on the product container was added to solve an issue for vildis...
		Make sure it works fine if you plan to remove or change them.
		    el.style.position = null;
		    el.style.top = null;
		    el.style.right = null;
		    el.style.bottom = null;
		    el.style.left = null;
		*/
		// [data-id]
		return "<script>
			function zlt__".$this->splide_id."__custom_slider() {

				if (".$use_switch_attributes.") {
					const zlt__switch_container = document.querySelector('#".$this->a_p_h."');
					var zlt__switch_matches = zlt__switch_container.querySelectorAll('".$switch_attributes_source."');
					Array.prototype.forEach.call(zlt__switch_matches, function(el, index, array) {
						el.setAttribute('".$switch_attributes_destination."', el.getAttribute('".trim($switch_attributes_source, '[]')."'));
					});
				}


				var __Ztp = document.getElementsByClassName('".$this->t_p_wrapper."')[0];
				document.getElementById('".$this->grids_wrapper."').removeAttribute('style');
				__Ztp.removeAttribute('style');

				__Ztp.insertAdjacentHTML('beforeend', `
					<style type=\"text/css\">

						.".$this->block_id." .splide__container {
							box-sizing: border-box;
							position: relative
						}

						.".$this->block_id." .splide__list {
							-webkit-backface-visibility: hidden;
							backface-visibility: hidden;
							display: -ms-flexbox;
							display: flex;
							height: 100%;
							margin: 0 !important;
							padding: 0 !important
						}

						.".$this->block_id." .splide.is-initialized:not(.is-active) .splide__list {
							display: block
						}

						.".$this->block_id." .splide__pagination {
							-ms-flex-align: center;
							align-items: center;
							display: -ms-flexbox;
							display: flex;
							-ms-flex-wrap: wrap;
							flex-wrap: wrap;
							-ms-flex-pack: center;
							justify-content: center;
							margin: 0;
							pointer-events: none
						}

						.".$this->block_id." .splide__pagination li {
							display: inline-block;
							line-height: 1;
							list-style-type: none;
							margin: 0;
							pointer-events: auto
						}

						.".$this->block_id." .splide__progress__bar {
							width: 0
						}

						.".$this->block_id." .splide {
							position: relative;
							visibility: hidden
						}

						.".$this->block_id." .splide.is-initialized,
						.".$this->block_id." .splide.is-rendered {
							visibility: visible
						}

						.".$this->block_id." .splide__slide {
							-webkit-backface-visibility: hidden;
							backface-visibility: hidden;
							box-sizing: border-box;
							-ms-flex-negative: 0;
							flex-shrink: 0;
							list-style-type: none !important;
							margin: 0;
							position: relative
						}

						.".$this->block_id." .splide__slide img {
							vertical-align: bottom
						}

						.".$this->block_id." .splide__spinner {
							animation: splide-loading 1s linear infinite;
							border: 2px solid #999;
							border-left-color: transparent;
							border-radius: 50%;
							bottom: 0;
							contain: strict;
							display: inline-block;
							height: 20px;
							left: 0;
							margin: auto;
							position: absolute;
							right: 0;
							top: 0;
							width: 20px
						}

						.".$this->block_id." .splide__sr {
							clip: rect(0 0 0 0);
							border: 0;
							height: 1px;
							margin: -1px;
							overflow: hidden;
							padding: 0;
							position: absolute;
							width: 1px
						}

						.".$this->block_id." .splide__toggle.is-active .splide__toggle__play,
						.".$this->block_id." .splide__toggle__pause {
							display: none
						}

						.".$this->block_id." .splide__toggle.is-active .splide__toggle__pause {
							display: inline
						}

						.".$this->block_id." .splide__track {
							overflow: hidden;
							position: relative;
							z-index: 0;
							width: 100%;
						}

						@keyframes splide-loading {
							0% {
								transform: rotate(0)
							}

							to {
								transform: rotate(1turn)
							}
						}

						.".$this->block_id." .splide__track--draggable {
							-webkit-touch-callout: none;
							-webkit-user-select: none;
							-ms-user-select: none;
							user-select: none
						}

						.".$this->block_id." .splide__track--fade>.splide__list {
							display: block
						}

						.".$this->block_id." .splide__track--fade>.splide__list>.splide__slide {
							left: 0;
							opacity: 0;
							position: absolute;
							top: 0;
							z-index: 0
						}

						.".$this->block_id." .splide__track--fade>.splide__list>.splide__slide.is-active {
							opacity: 1;
							position: relative;
							z-index: 1
						}

						.".$this->block_id." .splide--rtl {
							direction: rtl
						}

						.".$this->block_id." .splide__track--ttb>.splide__list {
							display: block
						}

						.".$this->block_id." .splide__arrow {
							-ms-flex-align: center;
							align-items: center;
							background: ".$zl_desktop_splide_arrow_background_color.";
							border: 0;
							border-radius: 50%;
							cursor: pointer;
							display: -ms-flexbox;
							display: flex;
							height: 2em;
							-ms-flex-pack: center;
							justify-content: center;
							opacity: .7;
							padding: 0;
							position: absolute;
							top: 50%;
							transform: translateY(-50%);
							width: 2em;
							z-index: 1;

							/* old styling */
							/* min-width: 10px !important; */
							/* -webkit-tap-highlight-color: transparent; */
							/* border-radius: 3px !important; */
							/* background: #b8adad73 !important; */
							/* color: #fcfafa !important; */
							/* top: 40% !important; */
							/* width: auto !important; */
							/* padding: 24px 5px !important; */
						}

						.".$this->block_id." .splide__arrow svg {
							/* fill: #201e1e; */
							/* font-size: 16px; */
							fill: ".$zl_desktop_splide_arrow_color.";
							height: 1.2em;
							width: 1.2em
						}

						.".$this->block_id." .splide__arrow:hover:not(:disabled) {
							opacity: .9
						}

						.".$this->block_id." .splide__arrow:disabled {
							opacity: .3
						}

						.".$this->block_id." .splide__arrow:focus-visible {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide__arrow--prev {
							left: 1em
							/* left: 0.3em !important; */
							/* justify-content:end !important; */
						}

						.".$this->block_id." .splide__arrow--prev svg {
							transform: scaleX(-1)
						}

						.".$this->block_id." .splide__arrow--next {
							right: 1em
							/* justify-content:flex-end !important; */
							/* right: 0.3em !important; */
						}

						.".$this->block_id." .splide.is-focus-in .splide__arrow:focus {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide__pagination {
							bottom: .5em;
							left: 0;
							padding: 0 1em;
							position: absolute;
							right: 0;
							z-index: 1
						}

						.".$this->block_id." .splide__pagination__page {
							background: ".$zl_desktop_splide_dots_color.";
							border: 0;
							border-radius: 50%;
							display: inline-block;
							height: 8px;
							margin: 3px;
							opacity: .7;
							padding: 0;
							position: relative;
							transition: transform .2s linear;
							width: 8px
						}

						.".$this->block_id." .splide__pagination__page.is-active {
							background: ".$zl_desktop_splide_dots_active_color.";
							transform: scale(1.4);
							z-index: 1
						}

						.".$this->block_id." .splide__pagination__page:hover {
							cursor: pointer;
							opacity: .9
						}

						.".$this->block_id." .splide__pagination__page:focus-visible {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide.is-focus-in .splide__pagination__page:focus {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide__progress__bar {
							background: #ccc;
							height: 3px
						}

						.".$this->block_id." .splide__slide {
							-webkit-tap-highlight-color: transparent
						}

						.".$this->block_id." .splide__slide:focus {
							outline: 0
						}

						@supports(outline-offset:-3px) {
							.".$this->block_id." .splide__slide:focus-visible {
								outline: 3px solid #0bf;
								outline-offset: -3px
							}
						}

						@media screen and (-ms-high-contrast:none) {
							.".$this->block_id." .splide__slide:focus-visible {
								border: 3px solid #0bf
							}
						}

						@supports(outline-offset:-3px) {
							.".$this->block_id." .splide.is-focus-in .splide__slide:focus {
								outline: 3px solid #0bf;
								outline-offset: -3px
							}
						}

						@media screen and (-ms-high-contrast:none) {
							.".$this->block_id." .splide.is-focus-in .splide__slide:focus {
								border: 3px solid #0bf
							}

							.".$this->block_id." .splide.is-focus-in .splide__track>.splide__list>.splide__slide:focus {
								border-color: #0bf
							}
						}

						.".$this->block_id." .splide__toggle {
							cursor: pointer
						}

						.".$this->block_id." .splide__toggle:focus-visible {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide.is-focus-in .splide__toggle:focus {
							outline: 3px solid #0bf;
							outline-offset: 3px
						}

						.".$this->block_id." .splide__track--nav>.splide__list>.splide__slide {
							border: 3px solid transparent;
							cursor: pointer
						}

						.".$this->block_id." .splide__track--nav>.splide__list>.splide__slide.is-active {
							border: 3px solid #000
						}

						.".$this->block_id." .splide__arrows--rtl .splide__arrow--prev {
							left: auto;
							right: 1em
						}

						.".$this->block_id." .splide__arrows--rtl .splide__arrow--prev svg {
							transform: scaleX(1)
						}

						.".$this->block_id." .splide__arrows--rtl .splide__arrow--next {
							left: 1em;
							right: auto
						}

						.".$this->block_id." .splide__arrows--rtl .splide__arrow--next svg {
							transform: scaleX(-1)
						}

						.".$this->block_id." .splide__arrows--ttb .splide__arrow {
							left: 50%;
							transform: translate(-50%)
						}

						.".$this->block_id." .splide__arrows--ttb .splide__arrow--prev {
							top: 1em
						}

						.".$this->block_id." .splide__arrows--ttb .splide__arrow--prev svg {
							transform: rotate(-90deg)
						}

						.".$this->block_id." .splide__arrows--ttb .splide__arrow--next {
							bottom: 1em;
							top: auto
						}

						.".$this->block_id." .splide__arrows--ttb .splide__arrow--next body {
							margin: 0px;
							padding: 0px;
							font-family: 'Mulish', Arial, sans-serif;
						}

						.".$this->block_id." .splide__arrow--next {
							right: -40px;
							/* justify-content:flex-end !important; */
							/* right: 0.3em !important; */
						}

						.".$this->block_id." .splide__arrow--prev {
							left: -40px;
							/* left: 0.3em !important; */
							/* justify-content:end !important; */
						}

						.".$this->block_id." .splide__arrow {
							background-color: ".$zl_desktop_splide_arrow_background_color.";
							opacity: 1;
							display: ".$zl_desktop_splide_use_arrows.";

							/* old styling */
							/* min-width: 10px !important; */
							/* -webkit-tap-highlight-color: transparent; */
							/* border-radius: 3px !important; */
							/* background: #b8adad73 !important; */
							/* color: #fcfafa !important; */
							/* top: 40% !important; */
							/* width: auto !important; */
							/* padding: 24px 5px !important; */
						}

						.".$this->block_id."  .splide__arrow svg {
							/* fill: #201e1e; */
							/* font-size: 16px; */
							fill: ".$zl_desktop_splide_arrow_color.";
						}

						.".$this->block_id." .splide__pagination {
								display: ".$zl_desktop_splide_use_dots.";
						}

						.".$this->block_id." .splide
						{
							visibility: visible
						}

						.".$this->block_id." .mobile-indicator
						{
							display: none;
							z-index: 0;
						}

						/** custom slide css **/

						.".$this->block_id." .splide__slide > div {
							width:100% !important;
						}

						@media screen and (max-width: 767px) {

							.".$this->block_id." .mobile-indicator {
								display: block;
								z-index: 1;
							}

							.".$this->block_id." .splide__arrow {
									-ms-flex-align: center;
									align-items: center;
									background: ".$zl_mobile_splide_arrow_background_color.";
									border: 0;
									border-radius: 50%;
									cursor: pointer;
									display: -ms-flexbox;
									display: flex;
									height: 2em;
									-ms-flex-pack: center;
									justify-content: center;
									opacity: .7;
									padding: 0;
									position: absolute;
									top: 50%;
									transform: translateY(-50%);
									width: 2em;
									z-index: 1;

									/* old styling */
									/* min-width: 10px !important; */
									/* -webkit-tap-highlight-color: transparent; */
									/* border-radius: 3px !important; */
									/* background: #b8adad73 !important; */
									/* color: #fcfafa !important; */
									/* top: 40% !important; */
									/* width: auto !important; */
									/* padding: 24px 5px !important; */
							}

							.".$this->block_id." .splide__arrow svg {
									fill: ".$zl_mobile_splide_arrow_color.";
									height: 1.2em;
									width: 1.2em
							}

							.".$this->block_id." .splide__pagination__page {
									background: ".$zl_mobile_splide_dots_color.";
									border: 0;
									border-radius: 50%;
									display: inline-block;
									height: 8px;
									margin: 3px;
									opacity: .7;
									padding: 0;
									position: relative;
									transition: transform .2s linear;
									width: 8px
							}

							.".$this->block_id." .splide__pagination__page.is-active {
									background: ".$zl_mobile_splide_dots_active_color.";
									transform: scale(1.4);
									z-index: 1
							}

							.".$this->block_id." .splide__arrow {
									background-color: ".$zl_mobile_splide_arrow_background_color.";
									opacity: 1;
									display: ".$zl_mobile_splide_use_arrows.";

									/* old styling */
									/* min-width: 10px !important; */
									/* -webkit-tap-highlight-color: transparent; */
									/* border-radius: 3px !important; */
									/* background: #b8adad73 !important; */
									/* color: #fcfafa !important; */
									/* top: 40% !important; */
									/* width: auto !important; */
									/* padding: 24px 5px !important; */
							}

							.".$this->block_id." .splide__arrow svg {
									fill: ".$zl_mobile_splide_arrow_color.";
							}

							.".$this->block_id." .splide__pagination {
									display: ".$zl_mobile_splide_use_dots.";
							}

							/** custom slide css **/

							.".$this->block_id." .splide__slide > div {
								width:100% !important;
							}

					}
					</style>
					<div id='".$this->splide_id."-indicator' class='".$this->splide_id." mobile-indicator'></div>
					<div id='".$this->splide_id."' class='splide'>
						<div class='splide__track'>
							<ul class='splide__list' style='display:flex !important;gap:0 !important'>
							</ul>
						</div>
					</div>`
				);

				setTimeout(function() {

					// Create the state-indicator element
					var zlt__indicator = document.getElementById('".$this->splide_id."-indicator');

					// Create a method which returns device state
					function getDeviceState() {
				    var zlt__index = parseInt(window.getComputedStyle(zlt__indicator).getPropertyValue('z-index'), 10);

				    var zlt__states = {
							0: 'desktop',
			        1: 'mobile'
				    };

				    return zlt__states[zlt__index] || 'desktop';
					}

					//setTimeout(function() {
					function zlt__deploy_slider() {

						var zl__original_ul = document.querySelectorAll('#".$this->a_p_h." .products');
						if (zl__original_ul.length < 1) {
							zl__original_ul = document.getElementById('".$this->a_p_h."').firstChild;
						} else {
							zl__original_ul = zl__original_ul[0];
						}

						var els = document.querySelectorAll('#".$this->a_p_h." .product');
						document.getElementById('".$this->a_p_h."').style.height = '0px';
						document.getElementById('".$this->a_p_h."').style.display = '".$atts['a_p_h_display']."';
						var classList = els[0].classList[0];
						var __zHTml = '';

						var __Zsl = document.getElementById('".$this->splide_id."').getElementsByClassName('splide__list')[0];
						zl__original_ul.classList.forEach((item) => {
							if (item != 'columns-".$columns."') {
								__Zsl.classList.add(item);
							}
						});

						Array.prototype.forEach.call(els, function(el, index, array) {
							el.style.position = null;
							el.style.top = null;
							el.style.right = null;
							el.style.bottom = null;
							el.style.left = null;
							el.style.width = 'auto';
							el.style.height = '100%';
							if(els[0].tagName.toLowerCase() == 'li')
								__zHTml += '<li class=\"splide__slide tag_was_li ' + el.classList + ' \">' + el.innerHTML + '</li>';
							else
								__zHTml += '<li class=\"splide__slide tag_was_not_li ' + el.classList + ' \" style=\"display: flex;\">' + el.outerHTML + '</li>';
							setTimeout(function() { el.remove(); }, 75)
						});

						__Zsl.insertAdjacentHTML('beforeend', __zHTml);
						var ".$this->splide_id." = new Splide('#".$this->splide_id."', {
								perPage: ".$zl_desktop_splide_perPage.",
								gap: '".$zl_desktop_splide_gap."',
								type: 'loop',
								//autoWidth: true,
								breakpoints: {
										768: {
												perPage: ".$zl_mobile_splide_perPage.",
												gap: '".$zl_mobile_splide_gap."',
												type: 'loop',
												//autoWidth: true,
										}
								},
						});
						".$this->splide_id.".mount();


					//}, 0);
					};


					if (".$zl_desktop_use_splide." && ".$zl_mobile_use_splide.") {

							zlt__deploy_slider();

					} else if (".$zl_desktop_use_splide.") {

							if(getDeviceState() == 'desktop') {
									zlt__deploy_slider();
							}

					} else if (".$zl_mobile_use_splide.") {

							if(getDeviceState() == 'mobile') {
									zlt__deploy_slider();
							}

					}

				}, 0);
			}

			if (".$zl_desktop_use_splide." || ".$zl_mobile_use_splide.") {
				var script = document.createElement('script');
				script.onload = function() {
					zlt__".$this->splide_id."__custom_slider();
				};
				script.src = 'https://cdn.jsdelivr.net/npm/@splidejs/splide@4.0.7/dist/js/splide.min.js';
				script.setAttribute(\"type\", \"text/javascript\");
				document.body.appendChild(script);
			}
		</script>";

	}


	/**
	 * Parse attributes.
	 *
	 * @since  3.2.0
	 * @param  array $attributes Shortcode attributes.
	 * @return array
	 */
	protected function parse_attributes( $attributes ) {
		if (get_option('ebz_is_debug')){
			$debug = True;
		} else {
			$debug = False;
		}

		$attributes = $this->parse_legacy_attributes( $attributes );
		if(isset($attributes['header'])){
			$header = stripslashes($attributes['header']);
			$header = stripslashes($header);
		}else{
			$header = "";
		}

		$attributes = shortcode_atts(
			array(
				'limit'          					=> '',       // Results limit.
				'columns'        					=> '',       // Number of columns.
				'rows'           					=> '',        // Number of rows. If defined, limit will be ignored.
				'orderby'        					=> 'title',   // menu_order, title, date, rand, price, popularity, rating, or id.
				'order'          					=> 'ASC',     // ASC or DESC.
				'ids'            					=> '',        // Comma separated IDs.
				'skus'           					=> '',        // Comma separated SKUs.
				'category'       					=> '',        // Comma separated category slugs or ids.
				'cat_operator'   					=> 'IN',      // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'.
				'attribute'      					=> '',        // Single attribute slug.
				'terms'          					=> '',        // Comma separated term slugs or ids.
				'terms_operator' 					=> 'IN',      // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
				'tag'            					=> '',        // Comma separated tag slugs.
				'tag_operator'   					=> 'IN',      // Operator to compare tags. Possible values are 'IN', 'NOT IN', 'AND'.
				'visibility'     					=> 'visible', // Product visibility setting. Possible values are 'visible', 'catalog', 'search', 'hidden'.
				'class'          					=> '',        // HTML class.
				'page'           					=> 1,         // Page for pagination.
				'paginate'       					=> false,     // Should results be paginated.
				'cache'          					=> false,     // Should shortcode output be cached.
				'header'         					=> $header,
				'header_class'   					=> '',
				'block_style'    					=> 'max-width: 1170px;margin:0 auto',
				"data-mprods"							=> "4",
				"data-prods"							=> "4",
				"data-mslide"							=> "2",
				"data-slide"							=> "-1",
				'drop_data_attributes'  	=> '',        // Comma separated data attributes to remove.
				'data_attribute_elements'	=> 'div',     // Comma separated elements to search for data attributes to remove.
				'a_p_h_style'							=> 'display:flex;flex-wrap:wrap;justify-content:center;margin:0 auto;max-width:100%;width:100%;',
				'switch_attributes'				=> '', 				// E.g. 'srcset|src'
				'product_wrapper_class'					=> 'zl__product_wrapper_class'
			),
			$attributes,
			$this->type
		);

		/* No need for default count here
		if ( ! absint( $attributes['columns'] ) ) {
			$attributes['columns'] = wc_get_default_products_per_row();
		}*/

		return $attributes;
	}

	/**
	 * Parse legacy attributes.
	 *
	 * @since  3.2.0
	 * @param  array $attributes Attributes.
	 * @return array
	 */
	protected function parse_legacy_attributes( $attributes ) {
		$mapping = array(
			'per_page' => 'limit',
			'operator' => 'cat_operator',
			'filter'   => 'terms',
		);

		foreach ( $mapping as $old => $new ) {
			if ( isset( $attributes[ $old ] ) ) {
				$attributes[ $new ] = $attributes[ $old ];
				unset( $attributes[ $old ] );
			}
		}

		return $attributes;
	}


	/**
	 * Get wrapper classes.
	 *
	 * @since  3.2.0
	 * @param  array $columns Number of columns.
	 * @return array
	 */
	protected function get_wrapper_classes( $columns ) {
		$classes = array( 'woocommerce' );

		if ( ! absint( $columns ) ) {
			$columns = wc_get_default_products_per_row();
		}

		if ( 'product' !== $this->type ) {
			$classes[] = 'columns-' . $columns;
		}

		$classes[] = $this->attributes['class'];

		return $classes;
	}



	/**
	 * Loop over found products.
	 *
	 * @since  3.2.0
	 * @return string
	 */
	protected function product_loop($products, $rec_settings) {
		if (get_option('ebz_is_debug')){
			$debug = True;
			var_dump($this->attributes);
		} else {
			$debug = False;
		}

		$rec_settings = $this->get_default_settings($rec_settings);

		$columns = $rec_settings['desktop']['products'];
		if (array_key_exists("columns", $this->attributes) && $this->attributes['columns'] != '') {
			$columns = absint( $this->attributes['columns'] );
		}

		$limit = $rec_settings['desktop']['products'];
		if (array_key_exists("limit", $this->attributes) && $this->attributes['limit'] != '') {
			$limit = absint( $this->attributes['limit'] );
		}

		$header_title = $rec_settings['title'];
		if (array_key_exists("header", $this->attributes) && $this->attributes['header'] != '') {
			$header_title = esc_attr( $this->attributes['header'] );
		}

    $header_class = esc_attr( $this->attributes['header_class'] );
    $block_style = esc_attr( $this->attributes['block_style'] );
		$classes  = $this->get_wrapper_classes( $columns );
		//$products = array(13,13,13,13);
		$drop_data_attributes = explode(',', esc_attr( trim($this->attributes['drop_data_attributes']) ));
		$data_attribute_elements = explode(',', esc_attr( trim($this->attributes['data_attribute_elements']) ));
		$a_p_h_style = esc_attr( $this->attributes['a_p_h_style'] );
		$product_wrapper_class = esc_attr( $this->attributes['product_wrapper_class'] );

    if (empty($products)) { return '';}

		ob_start();

		if ( $products ) {

			// Setup the loop.
			wc_setup_loop(
				array(
					'columns'      => $columns,
					'name'         => $this->type,
					'is_shortcode' => true,
					'is_search'    => false,
					'is_paginated' => false,
					'total'        => $limit,
					'total_pages'  => 1,
					'per_page'     => $limit,
					'current_page' => 1,
				)
			);

			$original_post = $GLOBALS['post'];

			do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes );

			// Fire standard shop loop hooks when paginating results so we can show result counts and so on.
			if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
				do_action( 'woocommerce_before_shop_loop' );
			}

			woocommerce_product_loop_start();

			if ( wc_get_loop_prop( 'total' ) ) {
        		foreach ($products as $product_id) {
					$GLOBALS['post'] = get_post( $product_id ); // WPCS: override ok.
					setup_postdata( $GLOBALS['post'] );
					wc_get_template_part( 'content', 'product' );
				}
			}

			$GLOBALS['post'] = $original_post; // WPCS: override ok.
			woocommerce_product_loop_end();

			// Fire standard shop loop hooks when paginating results so we can show result counts and so on.
			if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
				do_action( 'woocommerce_after_shop_loop' );
			}

			do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );

			wp_reset_postdata();
			wc_reset_loop();
		} else {
			do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes );
		}

		if ($debug) {
			var_dump($a_p_h_style);
		}

    $str_rep = '<div class="' . esc_attr( implode( ' ', $classes ) ) . '" style="'.$block_style.'"><div class="'.$header_class.'"><h3 style="margin: 40px 0 20px 0;">'.$header_title.'</h3></div><div id="'.$this->grids_wrapper.'"><div class="'.$this->t_p_wrapper.' related-products"><div id="'.$this->a_p_h.'" class="'.$this->block_id.' '.$product_wrapper_class.'" style="'.$a_p_h_style.'">' . ob_get_clean() . '</div></div></div></div>';

    $doc = new DOMDocument();

    // set error level
    $internalErrors = libxml_use_internal_errors(true);
    $doc->loadHTML(mb_convert_encoding($str_rep, 'HTML-ENTITIES', 'UTF-8'));
    // Restore error level
    libxml_use_internal_errors($internalErrors);

    foreach($doc->getElementsByTagName('a') as $link) {
       $oldLink = $link->getAttribute("href");
       if (strpos($oldLink,'?') !== false) {
           $link->setAttribute('href',  $oldLink."&zlt_source=v1&zltid=".$this->zltid);
       } else {
           $link->setAttribute('href',  $oldLink."?zlt_source=v1&zltid=".$this->zltid);
       }
    }
	try {
		foreach($drop_data_attributes as $dda) {
			foreach($data_attribute_elements as $dae) {
				foreach($doc->getElementsByTagName(trim($dae)) as $entity) {
				   $entity->removeAttribute(trim($dda));
				}
			}
		}
	} catch (Exception $e) {
		if ($debug) {
			var_dump($e);
		}
	}

    // remove <!DOCTYPE
    $doc->removeChild($doc->doctype);
    $str_rep = $doc->saveHTML();
    // remove <html><body></body></html>
    $str_rep = str_replace('<html><body>', '', $str_rep);
    $str_rep = str_replace('</body></html>', '', $str_rep);

		return '<div class="zlt '.$this->block_id.'" id="'.$this->block_id.'" data-zltid="'.$this->zltid.'"  >'.$str_rep.'</div>';
	}

}
