<?php

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

class AIEO_SEO_Location_Page {

	
	private static $cache = array();

	
	public static function url_for( array $loc ) {
		$id = (int) ( $loc['id'] ?? 0 );
		if ( $id <= 0 ) {
			return '';
		}
		if ( isset( self::$cache[ $id ] ) ) {
			return self::$cache[ $id ];
		}
		$url = '';

		
		if ( post_type_exists( 'aieo_store' ) ) {
			$ids = get_posts( array(
				'post_type'        => 'aieo_store',
				'post_status'      => 'publish',
				'numberposts'      => 1,
				'fields'           => 'ids',
				'meta_key'         => '_aieo_grv_location_id', 
				'meta_value'       => $id,                     
				'suppress_filters' => false,
				'no_found_rows'    => true,
			) );
			if ( ! empty( $ids ) ) {
				$url = (string) get_permalink( (int) $ids[0] );
			}
		}

		
		if ( '' === $url && function_exists( 'roosterx_store_post_url_for_location' ) ) {
			$themed = roosterx_store_post_url_for_location( $loc );
			if ( is_string( $themed ) ) {
				$url = $themed;
			}
		}

		self::$cache[ $id ] = $url;
		return $url;
	}
}
