<?php

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

$service_id = $args['p_id'] ?? get_the_ID();


$service_locations = gigneo_post_meta( $service_id, '_gigneo_pm_locations' );

if ( empty( $service_locations ) || ! is_array( $service_locations ) ) {
	return;
}

$service_type          = gigneo_service_type( $service_id );
$service_type_text     = esc_html__( 'Remote', 'gigneo' );
$service_delivery_type = gigneo_post_meta( $service_id, '_gigneo_pm_delivery_type' );

if ( 'onsite' === $service_type ) {
	if ( 'both' === $service_delivery_type ) {
		$service_type_text = esc_html__( 'Onsite - Hybrid', 'gigneo' );
	} elseif ( 'visit_me' === $service_delivery_type ) {
		$service_type_text = sprintf(
				// translators: %s is the freelancer singular
			esc_html__( 'Onsite - To %s location', 'gigneo' ),
			esc_html( gigneo_freelancer_singular() )
		);
	} elseif ( 'on_location' === $service_delivery_type ) {
		$service_type_text = esc_html__( 'Onsite - To Your location', 'gigneo' );
	}
}

$distance_type     = gigneo_option( '_gigneo_ts_map_distance_type', 'kilometers' );
$max_radius_option = gigneo_option( '_gigneo_ts_map_radius', '10' );

$map_enabled   = false;
$map_locations = array();

$countries = gigneo_allowed_countries();
$states    = gigneo_allowed_states();
?>
<div class="gn-section gn-locations-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Locations', 'gigneo' ); ?></h3>
		<span class="gn-type"><?php echo esc_html( $service_type_text ); ?></span>
	</div>
	<div class="gn-body gn-locations">
		<?php
		foreach ( $service_locations as $address ) {
			$country_state = explode( ':', $address['country_state'], 2 );
			$country       = $country_state[0] ?? '';
			$city_term     = get_term_by( 'slug', $address['city'] ?? '', 'gigneo_city' );
			$city_name     = $city_term->name ?? '';
			$location      = $address['location'][0] ?? array();
			$max_radius    = $address['max_radius'] ?? $max_radius_option;
			?>
			<div class="gn-location">
				<span class="gn-icon">
					<i class="hi hi-map-pin" aria-hidden="true"></i>
				</span>
				<div class="gn-info">
					<h6 class="gn-country-state">
						<?php
						echo esc_html( implode( ', ', array_filter( array( $city_name ?? '', $states[ $address['country_state'] ] ?? '', $countries[ $country ] ?? '' ) ) ) );
						?>
					</h6>
					<p class="gn-address"><?php echo esc_html( implode( ', ', array_filter( array( $address['area'] ?? '', $address['zip_code'] ?? '', $address['address'] ?? '' ) ) ) ); ?></p>
				</div>
			</div>
			<?php
		}
		?>
	</div>
</div>