<?php
/**
 * This file is a view file for Content -> Social Icons widget.
 *
 * @package CodexShaper Framework
 * @since CodexShaper Framework 1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
$buttons = $settings['share_buttons'] ?? array();

if ( empty( $buttons ) ) {
	return;
}

$social_share_items = array();

foreach ( $buttons as $button ) {
	$is_show_label = 'yes' === $settings['show_label'] || 'text' === $settings['view'];
	$has_icon      = 'icon' === $settings['view'] || 'icon-text' === $settings['view'];
	$share_icon    = $button['share_icon'] ?? null;
	$share_icons   = $instance->maybe_render_icon( $share_icon );
	$item_id       = $button['_id'];
	$social_link[] = $button['social_link']['url'] ?? '#';
	$instance->add_render_attribute( "social_share_item_wrapper_{$item_id}", 'class', 'csmf--social-share-item-wrapper' );
	$instance->add_render_attribute( "social_share_item_{$item_id}", 'class', array( 'csmf--social-share-item', 'csmf--social-share-' ) );
	$instance->add_render_attribute( "social_share_item_link_{$item_id}", 'class', array( 'social_link', 'elementor-repeater-item-' . $item_id ) );
	$instance->add_render_attribute( "social_share_item_{$item_id} ", 'data-social' );

	// Add item data to the array.
	$social_share_items[] = array(
		'wrapper_attr'   => $instance->get_render_attribute_string( "social_share_item_wrapper_{$item_id}" ),
		'item_attr'      => $instance->get_render_attribute_string( "social_share_item_{$item_id}" ),
		'item_link_attr' => $instance->get_render_attribute_string( "social_share_item_link_{$item_id}" ),
		'share_icons'    => $share_icons,
		'has_icon'       => $has_icon,
		'show_text'      => $show_text ?? false,
		'is_show_label'  => $is_show_label,
		'text'           => $button['text'] ?? '',
		'item_id'        => $item_id,
	);
}

?>
<div class="csmf--social-share-item-wrapper">

	<div class="csmf--social-share-content-wrapper">
		<?php foreach ( $social_share_items as $key => $item ) : ?>
			<div <?php echo wp_kses_post( $item['wrapper_attr'] ); ?>>
				<a <?php echo wp_kses_post( $item['item_link_attr'] ); ?> href="<?php echo esc_url( $social_link[ $key ] ); ?>">
					<div <?php echo wp_kses_post( $item['item_attr'] ); ?>>
						<?php if ( ( $item['text'] && $item['is_show_label'] ) ) : ?>
							<div class="csmf--social-share-item-content">
								<?php if ( $item['is_show_label'] ) : ?>
									<span class="csmf--social-share-item-title">
										<?php echo esc_html( $item['text'] ); ?>
									</span>
								<?php endif; ?>
							</div>
						<?php endif ?>
						<?php if ( $item['share_icons'] && $item['has_icon'] ) : ?>
							<span class="csmf--social-share-item-icon">
								<?php echo wp_kses( $item['share_icons'], csmf_get_svg_rules() ); ?>
							</span>
						<?php endif; ?>
					</div>
				</a>
			</div>
		<?php endforeach; ?>
	</div>
</div>
<?php
