<?php
declare(strict_types=1);

namespace Ekesto\CacheInvalidator\Resolvers;

use Ekesto\CacheInvalidator\Integrations\Structural\Core\StructuralContainerResolver;
use Ekesto\CacheInvalidator\UsageResolutionResult;

/**
 * Resolves content entities that reference a navigation entity.
 */
final class NavigationUsageResolver
{
    /**
     * Resolve front-end content IDs that reference the given navigation entity.
     *
     * @param int $navigationId Navigation post ID.
     * @return array{post_ids:int[],has_references:bool,uncertain:bool,uncertain_due_to_context:bool,uncertain_due_to_scan_gap:bool,direct_matches_count:int,structural_matches_count:int}
     */
    public static function resolveReferencedPostIds(int $navigationId): array
    {
        $navigationId = absint($navigationId);
        $navigation = get_post($navigationId);
        if (!$navigation instanceof \WP_Post) {
            return (new UsageResolutionResult([], false, false, true, 0, 0))->toArray();
        }

        return (new StructuralContainerResolver())->resolveReferencedPostIds($navigation);
    }
}
