<?php

/**
 * A collection of helper methods to simplify and standardize common tasks
 * related to WordPress functionalities within the RadiusBooking plugin.
 */

namespace RadiusTheme\RadiusBooking\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Class WPHelpers provides utility methods for handling WordPress user data
 * and formatting country names.
 */
class WPHelpers {
	/**
	 * Retrieves the data of the currently logged-in user.
	 *
	 * @return object The data of the current user as an object.
	 */
	public static function get_current_user_data() {
		return wp_get_current_user()->data;
	}

	/**
	 * Retrieves a list of formatted country names.
	 *
	 * Applies a filter to retrieve a list of countries, formats each country into an array with 'value'
	 * as the country code and 'label' as the country title, and returns the processed list.
	 *
	 * @return array A list of countries, each represented as an associative array with 'value' and 'label' keys.
	 */
	public static function get_formated_country_name() {
		$countries = apply_filters( 'radius_booking_countries', get_rtrb_country_name() );

		return array_map(
			function ( $country ) {
				return array(
					'value' => $country['code'],
					'label' => $country['title'],
				);
			},
			$countries
		);
	}

	/**
	 * Get all published pages formatted for select options.
	 *
	 * @return array
	 */
	public static function get_wp_pages_for_select(): array {
		$pages = get_pages(
			array(
				'post_status' => 'publish',
				'sort_column' => 'post_title',
				'sort_order'  => 'ASC',
			)
		);

		$result = array(
			array(
				'value' => 0,
				'label' => __( '-- Select a page --', 'radius-booking' ),
			),
		);

		foreach ( $pages as $page ) {
			if( $page->post_title == ''){
				continue;
			}
			$result[] = array(
				'value' => $page->ID,
				'label' => $page->post_title,
			);
		}

		return $result;
	}
}
