<?php

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

$j_search           = $args['search'] ?? '';
$j_location         = $args['location'] ?? '';
$j_category         = $args['category'] ?? '';
$j_sub_category     = $args['subcategory'] ?? '';
$j_specific_types   = $args['specific_types'] ?? '';
$j_skills           = (array) ( $args['skills'] ?? array() );
$j_budget_min       = $args['budget_min'] ?? '';
$j_budget_max       = $args['budget_max'] ?? '';
$j_mode             = $args['mode'] ?? '';
$j_amount_spent     = $args['amount_spent'] ?? '';
$j_experience_level = $args['experience_level'] ?? '';
$j_map_address      = $args['address'] ?? '';
$j_map_location     = $args['map_location'] ?? '';

$active_pills = array();

$current_url = ! empty( $args['current_url'] ) ? $args['current_url'] : remove_query_arg( array( 'paged', 'step' ) );

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

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

// Map pill.
if ( ! empty( $j_map_address ) ) {
	$active_pills[] = array(
		'label' => $j_map_address,
		'url'   => remove_query_arg( array( 'address', 'map_location' ), $current_url ),
	);
}

// Category pill.
if ( ! empty( $j_category ) ) {
	$cat_term       = get_term_by( 'slug', $j_category, 'gigneo_facet' );
	$active_pills[] = array(
		'label' => $cat_term ? $cat_term->name : $j_category,
		'url'   => remove_query_arg( array( 'category', 'subcategory', 'specific_types' ), $current_url ),
	);
}

if ( ! empty( $j_sub_category ) ) {
	$sub_cat_term   = get_term_by( 'slug', $j_sub_category, 'gigneo_facet' );
	$active_pills[] = array(
		'label' => $sub_cat_term ? $sub_cat_term->name : $j_sub_category,
		'url'   => remove_query_arg( array( 'subcategory', 'specific_types' ), $current_url ),
	);
}

if ( ! empty( $j_specific_types ) && is_array( $j_specific_types ) ) {
	foreach ( $j_specific_types as $sp_type ) {
		$sp_term   = get_term_by( 'slug', $sp_type, 'gigneo_facet' );
		$remaining = array_diff( $j_specific_types, array( $sp_type ) );
		$pill_url  = remove_query_arg( 'specific_types', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'specific_types', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $sp_term ? $sp_term->name : $sp_type,
			'url'   => $pill_url,
		);
	}
}

// Skills pills.
if ( ! empty( $j_skills ) && is_array( $j_skills ) ) {
	foreach ( $j_skills as $skill_slug ) {
		$skill_term = get_term_by( 'slug', $skill_slug, 'gigneo_skill' );
		$remaining  = array_diff( $j_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,
		);
	}
}

// Rate range pill.
$has_rate_min = '' !== $j_budget_min && floatval( $j_budget_min ) > 0;
$has_rate_max = '' !== $j_budget_max && floatval( $j_budget_max ) < 5000;

if ( $has_rate_min || $has_rate_max ) {
	$rate_label = '';
	if ( $has_rate_min && $has_rate_max ) {
		$rate_label = gigneo_price_format( $j_budget_min, true ) . ' - ' . gigneo_price_format( $j_budget_max, true ) . '/hr';
	} else {
		$rate_label = esc_html__( 'Up to ', 'gigneo' ) . gigneo_price_format( $j_budget_max, true );
	}
	$active_pills[] = array(
		'label' => $rate_label,
		'url'   => remove_query_arg( array( 'budget_min', 'budget_max' ), $current_url ),
	);
}
// Mode pill.
if ( ! empty( $j_mode ) ) {
	$active_pills[] = array(

		'label' => sprintf(
			// translators: %s is the job mode, e.g. remote, onsite.
			esc_html__( 'Mode: %s', 'gigneo' ),
			esc_html( ucfirst( $j_mode ) )
		),
		'url'   => remove_query_arg( 'mode', $current_url ),
	);
}

// Amount spent pill.
if ( ! empty( $j_amount_spent ) ) {
	$active_pills[] = array(
		'label' => sprintf(
				// translators: %s is amount spent
			esc_html__( '%s+ spent', 'gigneo' ),
			gigneo_price_format( $j_amount_spent, true )
		),
		'url'   => remove_query_arg( 'amount_spent', $current_url ),
	);
}

// Experience level pill.
if ( ! empty( $j_experience_level ) ) {
	$active_pills[] = array(
		'label' => sprintf(
			// translators: %s is the experience level.
			esc_html__( 'Experience level: %s', 'gigneo' ),
			esc_html( $j_experience_level )
		),
		'url'   => remove_query_arg( 'experience_level', $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
}
