<?php

/**
 * vcex_split_heading shortcode output
 *
 * @package Total WordPress Theme
 * @subpackage Total Theme Core
 * @version 2.7
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $atts['strings'] ) ) {
	return;
}

$strings = (array) vcex_vc_param_group_parse_atts( $atts['strings'] );

if ( ! $strings ) {
	return;
}

$direction = ( isset( $atts['direction'] ) && 'vertical' === $atts['direction'] ) ? 'vertical' : 'horizontal';
$tag = ! empty( $atts['tag'] ) ? $atts['tag'] : get_theme_mod( 'vcex_heading_default_tag', 'div' );
$tag = apply_filters( 'vcex_heading_default_tag', $tag );
$tag_safe = ( $tag_escape = tag_escape( $tag ) ) ? $tag_escape : 'div';
$last_index = count( $strings ) - 1;

$wrap_class = [
	'vcex-split-heading',
	"vcex-split-heading--{$direction}",
	'vcex-heading',
	'vcex-module',
	'wpex-heading',
	'wpex-text-2xl',
];

if ( 'vertical' === $direction ) {
	$wrap_class[] = 'wpex-grid wpex-grid-cols-1';
}

if ( ! empty( $atts['align_items'] ) ) {
	$wrap_class[] = vcex_parse_align_items_class( $atts['align_items'] );
}

if ( ! empty( $atts['justify_items'] ) ) {
	$wrap_class[] = vcex_parse_justify_items_class( $atts['justify_items'] );
}

if ( ! empty( $atts['max_width'] ) ) {
	$align = ! empty( $atts['align'] ) ? $atts['align'] : 'center';
	$wrap_class[] = vcex_parse_align_class( $align );
}

$extra_classes = vcex_get_shortcode_extra_classes( $atts, 'vcex_split_heading' );

if ( $extra_classes ) {
	$wrap_class = array_merge( $wrap_class, $extra_classes );
}

$wrap_class = vcex_parse_shortcode_classes( $wrap_class, 'vcex_split_heading', $atts );

// Element output starts here
$output = '<' . $tag_safe . ' class="' . esc_attr( $wrap_class ) . '">';

	// Loop through strings
	foreach ( $strings as $string_index => $string ) {
		if ( empty( $string['text'] ) ) {
			continue;
		}

		// Single string outer style
		$outer_style_args = [];
		if ( ! empty( $string['margin_end'] ) ) {
			$margin_end_prop = ( 'horizontal' === $direction ) ? 'margin_right' : 'margin_bottom';
			$outer_style_args[ $margin_end_prop ] = $string['margin_end'];
		}
		$outer_style = $outer_style_args ? vcex_inline_style( $outer_style_args ) : '';

		// Single string outer classes
		$outer_span_class = 'vcex-split-heading__item wpex-inline-block';
		if ( ! empty( $string['el_class'] ) ) {
			$outer_span_class .= ' ' . sanitize_text_field( $string['el_class'] );
		}

		// Output single string
		$output .= '<span class="' . esc_attr( $outer_span_class ) . '"' . $outer_style .'>';

				// Single string inner style
				$inner_span_style = vcex_inline_style( [
					'color'          => $string['color'] ?? null,
					'padding'        => $string['padding'] ?? null,
					'background'     => $string['background'] ?? null,
					'rotate'         => $string['rotate'] ?? null,
					'font_style'     => $string['font_style'] ?? null,
					'font_weight'    => $string['font_weight'] ?? null,
					'font_family'    => $string['font_family'] ?? null,
					'opacity'        => $string['opacity'] ?? null,
					'border_radius'  => $string['border_radius'] ?? null,
					'letter_spacing' => $string['letter_spacing'] ?? null,
				] );
			// Output single string inner span and content
			$output .= '<span class="vcex-split-heading__item-inner wpex-inline-block"' . $inner_span_style . '>' . vcex_parse_text_safe( $string['text'] ) . '</span>';
		$output .= '</span>';
		if ( 'horizontal' === $direction && $last_index !== $string_index ) {
			$output .= ' ';
		}
	}
$output .= '</' . $tag_safe . '>';

// @codingStandardsIgnoreLine
echo $output;
