<?php

namespace SugarCalendarBookings\Locations;

/**
 * Class LocationsHelpers.
 *
 * @since 1.0.0
 */
class LocationsHelpers {

	/**
	 * Format an address for display.
	 *
	 * @since 1.0.0
	 *
	 * @param LocationModel|null $location Location model instance.
	 *
	 * @return string
	 */
	public static function format_address( $location ) {

		// Return empty string for null locations (e.g., online locations, deleted locations).
		if ( ! $location ) {
			return '';
		}

		$parts = [
			$location->address,
			$location->address2,
			$location->city,
			$location->region,
			$location->postal_code,
		];

		return implode( ', ', array_filter( $parts ) );
	}

	/**
	 * Get Google Maps URL.
	 *
	 * @since 1.0.0
	 *
	 * @param LocationModel|null $location Location model instance.
	 *
	 * @return string
	 */
	public static function get_google_maps_url( $location ) {

		$address = static::format_address( $location );

		// Return empty string if no address (e.g., online locations, deleted locations).
		if ( empty( $address ) ) {
			return '';
		}

		return add_query_arg( 'q', $address, 'https://www.google.com/maps' );
	}
}
