<?php

/**
 * Layout: List.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$args = array(
	'taxonomy'         => 'aiopod_categories',      
	'orderby'          => sanitize_text_field( $attributes['orderby'] ),
	'order'            => sanitize_text_field( $attributes['order'] ),
	'hide_empty'       => (int) $attributes['hide_empty'], 
	'hierarchical'     => (int) $attributes['hierarchical'],                
	'show_count'       => (int) $attributes['show_count'], 
	'show_option_none' => '',   
	'title_li'         => '',
	'echo'             => 0
);

if ( ! empty( $attributes['include'] ) ) { // Include category IDs
	$args['include'] = array_map( 'intval', explode( ',', $attributes['include'] ) );
} else {
	if ( $args['hierarchical'] ) {
		$args['child_of'] = (int) $attributes['id'];
	} else {
		$args['parent'] = (int) $attributes['id'];
	}				
}

if ( ! empty( $attributes['exclude'] ) ) { // Exclude category IDs
	$args['exclude'] = array_map( 'intval', explode( ',', $attributes['exclude'] ) );
}

$args = apply_filters( 'aiopod_categories_args', $args, $attributes );
$categories_li = wp_list_categories( $args ); 

// Render HTML output
if ( ! empty( $categories_li ) ) : ?>
	<div class="aiopod aiopod-categories aiopod-categories--layout-list">
		<!-- Title -->
		<?php if ( ! empty( $attributes['title'] ) ) : ?>
			<div class="aiopod-categories__title aiopod-text-heading">
				<?php echo esc_html( $attributes['title'] ); ?>
			</div>
		<?php endif; ?>
		
		<ul class="aiopod-categories__list">
			<?php echo $categories_li; ?>
		</ul>
	</div>
<?php elseif ( 0 === (int) $attributes['hide_if_empty'] ) : ?>
	<div class="aiopod aiopod-categories aiopod-categories--layout-list">
		<div class="aiopod-categories__empty aiopod-text-muted">
			<?php esc_html_e( 'No categories found.', 'aio-podcast-studio' ); ?>
		</div>
	</div>
<?php endif;
