<?php

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

$is_active         = $current === $term->slug;
$url               = add_query_arg( $taxonomy, $term->slug, $base_url );
$has_children      = ! empty( $term->children );
$total_count       = isset( $term->total_count ) ? (int) $term->total_count : (int) $term->count;
$expand_for_active = $has_children && ! empty( $term->has_active_descendant );
?>
<li class="wpps-folder-item <?php echo $has_children ? 'wpps-folder-has-children' : ''; ?> <?php echo $expand_for_active ? 'wpps-folder-expanded' : ''; ?>" data-term-id="<?php echo esc_attr( (string) $term->term_id ); ?>" data-slug="<?php echo esc_attr( $term->slug ); ?>">
	<div class="wpps-folder-row">
		<?php if ( current_user_can( 'manage_categories' ) && $is_top_level ) : ?>
			<span class="wpps-folder-drag-handle dashicons dashicons-menu" aria-label="<?php esc_attr_e( 'Drag to reorder', 'wp-powersuite' ); ?>" title="<?php esc_attr_e( 'Drag to reorder', 'wp-powersuite' ); ?>"></span>
		<?php endif; ?>
		<a href="<?php echo esc_url( $url ); ?>" class="wpps-folder-link <?php echo $is_active ? 'wpps-nav-active' : ''; ?>" draggable="false">
			<span class="dashicons dashicons-category"></span>
			<span class="wpps-folder-name-wrap">
				<span class="wpps-folder-name"><?php echo esc_html( $term->name ); ?></span>
				<span class="wpps-count">(<?php echo (int) $total_count; ?>)</span>
			</span>
		</a>
		<?php if ( $has_children ) : ?>
			<button type="button" class="wpps-folder-toggle dashicons dashicons-arrow-right" aria-label="<?php esc_attr_e( 'Expand/Collapse', 'wp-powersuite' ); ?>" title="<?php esc_attr_e( 'Expand/Collapse', 'wp-powersuite' ); ?>"></button>
		<?php endif; ?>
		<?php if ( current_user_can( 'manage_categories' ) ) : ?>
			<span class="wpps-folder-actions">
				<button type="button" class="wpps-add-subfolder wpps-folder-action-btn" data-term-id="<?php echo esc_attr( (string) $term->term_id ); ?>" aria-label="<?php esc_attr_e( 'Add subfolder', 'wp-powersuite' ); ?>" title="<?php esc_attr_e( 'Add subfolder', 'wp-powersuite' ); ?>"><span class="dashicons dashicons-plus-alt2"></span></button>
				<button type="button" class="wpps-folder-edit wpps-folder-action-btn" data-term-id="<?php echo esc_attr( (string) $term->term_id ); ?>" data-name="<?php echo esc_attr( $term->name ); ?>" data-parent="<?php echo esc_attr( (string) $term->parent ); ?>" aria-label="<?php esc_attr_e( 'Edit', 'wp-powersuite' ); ?>" title="<?php esc_attr_e( 'Edit', 'wp-powersuite' ); ?>"><span class="dashicons dashicons-edit"></span></button>
				<button type="button" class="wpps-folder-delete wpps-folder-action-btn" data-term-id="<?php echo esc_attr( (string) $term->term_id ); ?>" aria-label="<?php esc_attr_e( 'Delete', 'wp-powersuite' ); ?>" title="<?php esc_attr_e( 'Delete', 'wp-powersuite' ); ?>"><span class="dashicons dashicons-trash"></span></button>
			</span>
		<?php endif; ?>
	</div>
	<?php if ( $has_children ) : ?>
		<ul class="wpps-folder-children" role="group" aria-hidden="<?php echo $expand_for_active ? 'false' : 'true'; ?>">
			<?php
			foreach ( $term->children as $child ) {
				$term         = $child;
				$is_top_level = false;
				require __DIR__ . '/media-sidebar-folder-item.php';
			}
			?>
		</ul>
	<?php endif; ?>
</li>
