<?php

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

$f_search           = $args['search'] ?? '';
$f_location         = $args['location'] ?? '';
$f_categories       = $args['categories'] ?? array();
$f_skills           = $args['skills'] ?? array();
$f_rating           = $args['rating'] ?? '';
$f_amount_spent     = $args['amount_spent'] ?? '';
$f_online           = $args['online'] ?? '';
$f_verified         = $args['verified'] ?? '';
$f_orders_completed = $args['orders_completed'] ?? '';
$f_map_address      = $args['address'] ?? '';
$f_map_location     = $args['map_location'] ?? '';
$current_url        = $args['current_url'] ?? '';
$is_map_enabled     = apply_filters( 'gigneo_is_map_enabled', false );

if ( ! empty( $f_search ) ) {
	$active_pills[] = array(
		'label' => sprintf(
			// translators: %s is the search term entered by the user.
			esc_html__( 'Search: %s', 'gigneo' ),
			esc_html( $f_search )
		),
		'url'   => remove_query_arg( 'search', $current_url ),
	);
}

// Location pill.
if ( ! empty( $f_location ) ) {
	$location_label = gigneo_get_location_label( $f_location );
	$active_pills[] = array(
		'label' => $location_label,
		'url'   => remove_query_arg( 'location', $current_url ),
	);
}

if ( ! empty( $f_map_address ) ) {
	$active_pills[] = array(
		'label' => $f_map_address,
		'url'   => remove_query_arg( array( 'address', 'map_location' ), $current_url ),
	);
}

// Skills pills.
if ( ! empty( $f_skills ) && is_array( $f_skills ) ) {
	foreach ( $f_skills as $skill_slug ) {
		$skill_term = get_term_by( 'slug', $skill_slug, 'gigneo_skill' );
		$remaining  = array_diff( $f_skills, array( $skill_slug ) );
		$pill_url   = remove_query_arg( 'skills', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'skills', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $skill_term ? $skill_term->name : $skill_slug,
			'url'   => $pill_url,
		);
	}
}

// Categories pills.
if ( ! empty( $f_categories ) && is_array( $f_categories ) ) {
	foreach ( $f_categories as $category_slug ) {
		$category_term = get_term_by( 'slug', $category_slug, 'gigneo_facet' );
		$remaining     = array_diff( $f_categories, array( $category_slug ) );
		$pill_url      = remove_query_arg( 'categories', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'categories', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $category_term ? $category_term->name : $category_slug,
			'url'   => $pill_url,
		);
	}
}

// Rating pill.
if ( ! empty( $f_rating ) ) {
	$active_pills[] = array(
		'label' => sprintf(
			// translators: %s is the rating value selected by the user.
			esc_html__( '%s Stars & up', 'gigneo' ),
			$f_rating
		),
		'url'   => remove_query_arg( 'rating', $current_url ),
	);
}

// Amount spent pill.
if ( ! empty( $f_amount_spent ) ) {
	$active_pills[] = array(
		'label' => gigneo_price_format( $f_amount_spent, true ),
		'url'   => remove_query_arg( 'amount_spent', $current_url ),
	);
}

// orders completed pill.
if ( ! empty( $f_orders_completed ) ) {
	$active_pills[] = array(
		'label' => sprintf(
		// translators: %s is orders completed
			esc_html__( '%s+ orders completed', 'gigneo' ),
			$f_orders_completed
		),
		'url'   => remove_query_arg( 'orders_completed', $current_url ),
	);
}

if ( ! empty( $f_online ) ) {
	$active_pills[] = array(
		'label' => esc_html__( 'Online', 'gigneo' ),
		'url'   => remove_query_arg( 'online', $current_url ),
	);
}

if ( ! empty( $f_verified ) ) {
	$active_pills[] = array(
		'label' => esc_html__( 'Verified', 'gigneo' ),
		'url'   => remove_query_arg( 'verified', $current_url ),
	);
}

if ( ! empty( $active_pills ) ) {
	?>
	<div class="gn-active-filters-pills">
		<?php foreach ( $active_pills as $pill ) { ?>
			<a href="<?php echo esc_url( $pill['url'] ); ?>" class="gn-filter-pill">
				<?php echo esc_html( $pill['label'] ); ?>
				<i class="hi hi-x-mark" aria-hidden="true"></i>
			</a>
		<?php } ?>
	</div>
	<?php
}
