<?php
/**
 * Context Resolver for Campaign Info Block.
 *
 * @package AmbassadorHub\Fundraisers\Blocks\CampaignInfo
 */

namespace AmbassadorHub\Fundraisers\Blocks\CampaignInfo;

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

use AmbassadorHub\Fundraisers\FundraisersHelpers;
use AmbassadorHub\Fundraisers\FundraisersModule;

/**
 * Resolves the root fundraiser context for the current fundraiser post.
 */
final class ContextResolver {
	/**
	 * Resolve root fundraiser context for the current request.
	 *
	 * @return array{campaign: ?\WP_Post}
	 */
	public static function resolve(): array {
		global $post;
		if ( ! $post || FundraisersModule::POST_TYPE !== get_post_type( $post ) ) {
			return array( 'fundraiser' => null );
		}
		$root_id   = FundraisersHelpers::get_root_fundraiser_id( $post->ID );
		$root_post = $root_id ? get_post( $root_id ) : null;
		if ( ! $root_post || FundraisersModule::POST_TYPE !== get_post_type( $root_post ) ) {
			return array( 'fundraiser' => null );
		}
		return array( 'fundraiser' => $root_post );
	}
}
