<?php
/**
 * Matador / Integrations / Bullhorn / Helper
 *
 * Various helper functions for Bullhorn
 *
 * @link        https://matadorjobs.com/
 * @since       3.9.0
 *
 * @package     Matador Jobs Board
 * @subpackage  Integrations / Bullhorn
 * @author      Matador Software LLC, Jeremy Scott, Paul Bearne
 * @copyright   (c) 2023, Matador Software, LLC
 *
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 */

namespace matador\MatadorJobs\Integrations\Bullhorn;

use matador\Matador;

class Helper {

	/**
	 * Get the countries from the cache so we don't load Bullhorn import
	 *
	 * @since 3.9.0
	 *
	 * @return array
	 */
	public static function get_country_list(): array {

		$cache_key = Matador::variable( 'bullhorn_countries_cache', 'transients' );

		return ( get_transient( $cache_key ) ) ?: [];
	}


	/**
	 * Skip the Application Country Field for Bullhorn when the Array of Countries is Empty
	 *
	 * @since 3.9.7
	 *
	 * @param bool   $skip Whether to skip the field.
	 * @param array  $args The field arguments.
	 * @param string $field The field name.
	 *
	 * @return bool
	 */
	public static function maybe_skip_application_country_field( bool $skip, array $args, string $field ): bool {

		if ( 'country' !== $field ) {

			return $skip;
		}

		if ( empty( $args['options'] ) ) {

			self::skip_application_country_field_message();

			return true;
		}

		return $skip;
	}

	/**
	 * Admin Message When Array of Bullhorn Countries is Empty
	 *
	 * @since 3.9.7
	 *
	 * @return void
	 */
	public static function skip_application_country_field_message(): void {

		if ( ! current_user_can( 'edit_posts' ) ) {

			return;
		}

		$format = "<div class='matador-admin-frontend-notice'><em>%s</em></div>";

		printf( $format, __( 'The country form field is skipped, as the Bullhorn Countries List is not synced. Please connect to and sync Bullhorn to download country data. This message is only shown to administrators.', 'matador-jobs' ) );
	}
}