<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;
use SmartPostShow\Blocks\Helper;

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

/**
 * Smart_Post_Tag block class.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */
class Smart_Post_Tag extends Block_Base {

	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name     = 'smart-post-tag';
		$this->scripts        = array( 'sp_smart_post_blocks_script_js' );
		$this->styles         = array( 'sp_smart_post_blocks_css', 'sp_smart_post_blocks_social_icons_style', 'sp_smart_post_blocks_google_fonts', 'sp_smart_post_blocks_style' );
		$this->editor_scripts = array( 'sp_smart_post_blocks_index_js' );
		$this->editor_styles  = array( 'sp_smart_post_blocks_editor_style' );
	}

	/**
	 * Render the block output on the frontend.
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content    Block content.
	 * @param array  $blocks     Inner blocks.
	 *
	 * @return string            Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}
		$is_license_activated = $this->is_license_activated();
		if ( 'valid' !== $is_license_activated ) {
			return $is_license_activated;
		}

		// Extract attributes with defaults.
		$unique_id                       = $attributes['uniqueId'] ?? '';
		$enable_icon                     = $attributes['enableIcon'] ?? true;
		$enable_heading                  = $attributes['enableHeading'] ?? true;
		$heading_label                   = $attributes['headingLabel'] ?? __( 'Categories:', 'smart-post-show-pro' );
		$separator                       = $attributes['separator'] ?? '';
		$custom_separator                = $attributes['customSeparator'] ?? '';
		$additional_css_class            = $attributes['advancedAdditionalClass'] ?? '';
		$icon_style                      = $attributes['iconStyle'] ?? 'default';
		$advanced_additional_id          = $attributes['advancedAdditionalID'] ?? '';
		$tag_global_typography           = $attributes['tagGlobalTypography'] ?? '';
		$tag_global_class                = $tag_global_typography['class'] ?? '';
		$heading_label_global_typography = $attributes['headingLabelGlobalTypography'] ?? '';
		$heading_label_global_class      = $heading_label_global_typography['class'] ?? '';

		if ( 'none' === $separator ) {
			$separator_html = '';
		} elseif ( 'custom-separator' === $separator ) {
			$separator_html = $custom_separator;
		} else {
			$separator_html = $separator;
		}

		unset(
			$attributes['dynamicCss'],
			$attributes['fontListsEditPage'],
			$attributes['fontLists']
		);

		$tags = array();

		$post_id = get_the_ID();

		if ( $post_id ) {
			$tags = get_the_tags( $post_id );
		}

		// If there are no tags, return empty string.
		if ( empty( $tags ) ) {
			return '';
		}

		$icon = Helper::get_icon_list( $icon_style );

		ob_start();
		?>


		
		<div id="<?php echo esc_attr( $advanced_additional_id ); ?>" class="sp-smart-post-wrapper <?php echo esc_attr( $additional_css_class ); ?>" >

			<div class="sp-smart-post-tag-container" id="<?php echo esc_attr( $unique_id ); ?>">

				<?php if ( $enable_icon || $enable_heading ) : ?>

					<div class="sp-smart-post-tag-label-container">

						<?php if ( $enable_icon ) : ?>

							<span class="sp-smart-post-tag-icon">
								
							
							<?php if ( ! empty( $icon ) ) { ?>
								<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">
					
										<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>
									
								</svg>
							<?php } ?>
						
						</span>

						<?php endif; ?>

						<?php if ( $enable_heading ) : ?>							
							<span class="sp-smart-post-tag-label <?php echo esc_attr( $heading_label_global_class ); ?>">
								<?php echo esc_html( $heading_label ); ?>
							</span>
						<?php endif; ?>

					</div>
				<?php endif; ?>

				<div class="sp-smart-post-tag-link-container <?php echo esc_attr( $tag_global_class ); ?>">

					<?php
					$total_tags = count( $tags );

					foreach ( $tags as $index => $tag ) :
						$tag_id   = absint( $tag->term_id );
						$tag_link = get_tag_link( $tag_id );
						$tag_name = $tag->name;
						$is_last  = ( ( $total_tags - 1 ) === $index );
						?>
						<a
							href="<?php echo esc_url( $tag_link ); ?>"
							rel="category tag"
							class="sp-smart-post-tag-link"
						>
							<?php echo esc_html( $tag_name ); ?>
						</a>
						<?php if ( ! $is_last ) : ?>
							<?php
							if ( 'none' !== $separator ) :
								?>
								<div class="sp-smart-post-separator" aria-hidden="true">
									<?php echo esc_html( $separator_html ); ?>
								</div>
							<?php endif; ?>
							<?php
						endif;
						?>


					<?php endforeach; ?>
				</div>

			</div>
		</div>

		<?php
		return ob_get_clean();
	}
}
