<?php
/**
 * Controller for rendering the block.
 * 
 * @since 3.0.0-beta.1
 * 
 * @package Spectra\Blocks\Countdown
 */

use Spectra\Helpers\BlockAttributes;

// Get the block attributes.
$end_date_time = $attributes['endDateTime'] ?? '';
$show_days     = $attributes['showDays'] ?? true;
$show_hours    = $attributes['showHours'] ?? true;
$show_minutes  = $attributes['showMinutes'] ?? true;
$show_seconds  = $attributes['showSeconds'] ?? true;

// If no end date-time is set or all time units are hidden, return empty string to prevent rendering.
if ( empty( $end_date_time ) || ( ! $show_days && ! $show_hours && ! $show_minutes && ! $show_seconds ) ) {
	return '';
}

// Extract additional block attributes with default fallback values.
$anchor         = $attributes['anchor'] ?? '';
$show_labels    = $attributes['showLabels'] ?? true;
$aria_live_type = $attributes['ariaLiveType'] ?? 'off';

// Set default singular and plural labels for each time unit, with translation support.
$day_label     = $attributes['dayLabel'] ?? __( 'Day', 'ultimate-addons-for-gutenberg' );
$days_label    = $attributes['daysLabel'] ?? __( 'Days', 'ultimate-addons-for-gutenberg' );
$hour_label    = $attributes['hourLabel'] ?? __( 'Hour', 'ultimate-addons-for-gutenberg' );
$hours_label   = $attributes['hoursLabel'] ?? __( 'Hours', 'ultimate-addons-for-gutenberg' );
$minute_label  = $attributes['minuteLabel'] ?? __( 'Minute', 'ultimate-addons-for-gutenberg' );
$minutes_label = $attributes['minutesLabel'] ?? __( 'Minutes', 'ultimate-addons-for-gutenberg' );
$second_label  = $attributes['secondLabel'] ?? __( 'Second', 'ultimate-addons-for-gutenberg' );
$seconds_label = $attributes['secondsLabel'] ?? __( 'Seconds', 'ultimate-addons-for-gutenberg' );

// Extract overflow and height attributes.
$overflow = $attributes['overflow'] ?? 'visible';
$height   = $attributes['height'] ?? 'auto';

// Style and class configurations.
$config = array(
	array(
		'key'        => 'overflow',
		'css_var'    => 'overflow',
		'class_name' => null,
		'value'      => $overflow,
	),
	array( 'key' => 'textColor' ),
	array( 'key' => 'textColorHover' ),
	array( 'key' => 'backgroundColor' ),
	array( 'key' => 'backgroundColorHover' ),
	array( 'key' => 'backgroundGradient' ),
	array( 'key' => 'backgroundGradientHover' ),
);

// Get layout settings.
$layout_type = $attributes['layout']['type'] ?? 'flex';
$block_gap   = $attributes['style']['spacing']['blockGap'] ?? null;

// Prepare custom classes for flow(default)/constrained layouts when blockGap is not set.
$additional_classes = array();
if ( ( 'default' === $layout_type || 'constrained' === $layout_type ) && is_null( $block_gap ) ) {
	$additional_classes[] = 'countdown-is-layout-flow-constrained';
}

// Prepare ARIA attributes based on ariaLiveType.
$aria_attributes = array(
	'id' => $anchor,
);

// Add ARIA attributes only when ariaLiveType is not 'off'.
if ( 'off' !== $aria_live_type ) {
	$aria_attributes['aria-live']   = $aria_live_type;
	$aria_attributes['aria-atomic'] = 'true';
	$aria_attributes['aria-label']  = __( 'Countdown timer', 'ultimate-addons-for-gutenberg' );
}

// Get the block wrapper attributes, and extend the styles and classes.
$wrapper_attributes = BlockAttributes::get_wrapper_attributes(
	$attributes,
	$config,
	$aria_attributes,
	$additional_classes,
);

// Default countdown context to initialize the countdown object in the store.
$default_countdown = array(
	'days'      => '00',
	'hours'     => '00',
	'minutes'   => '00',
	'seconds'   => '00',
	'isExpired' => false,
);

// Set the contexts required for the countdown wrapper.
$countdown_context = array(
	'endDateTime' => $end_date_time,
	'showDays'    => $show_days,
	'showHours'   => $show_hours,
	'showMinutes' => $show_minutes,
	'showSeconds' => $show_seconds,
	'labels'      => array(
		'dayLabel'     => $day_label,
		'daysLabel'    => $days_label,
		'hourLabel'    => $hour_label,
		'hoursLabel'   => $hours_label,
		'minuteLabel'  => $minute_label,
		'minutesLabel' => $minutes_label,
		'secondLabel'  => $second_label,
		'secondsLabel' => $seconds_label,
	),
	'countdown'   => $default_countdown,
);

/**
 * Filter the countdown context.
 * 
 * @since 3.0.0-beta.1
 * 
 * @param array $countdown_context The countdown context.
 * @param array $attributes       The block attributes.
 * @return array The modified countdown context.
 */
$countdown_context = apply_filters( 'spectra_countdown_context', $countdown_context, $attributes );

// Render the tabs block.
return 'file:./view.php';
