<?php

namespace StoreEngine\Shortcode;

use StoreEngine\Utils\Template;

class SingleProductGallery {

	public function __construct() {
		add_shortcode( 'storeengine_single_product_gallery', [ $this, 'render' ] );
	}

	public function render( array $attrs ) {
		$attributes = shortcode_atts( [
			'product_id' => 0,
		], $attrs );

		$product_id = absint( $attributes['product_id'] );
		if ( ! $product_id ) {
			$product_id = get_the_ID();
		}
		if ( ! $product_id ) {
			return '';
		}

		wp_enqueue_style(
			'flickity',
			STOREENGINE_ASSETS_URI . 'library/flickity/flickity.min.css',
			[],
			STOREENGINE_VERSION,
			null
		);
		wp_enqueue_script(
			'flickity',
			STOREENGINE_ASSETS_URI . 'library/flickity/flickity.pkgd.min.js',
			[],
			STOREENGINE_VERSION,
			true
		);

		ob_start();
		Template::get_template( 'single-product/gallery.php', [
			'product_id' => $product_id,
		] );

		return ob_get_clean();
	}

}
