<?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 synced pattern via core/block refs.
 */
final class PatternUsageResolver
{
    /**
     * Resolve front-end content IDs that reference the given synced pattern ID.
     *
     * @param int $patternId Synced pattern post ID (wp_block).
     * @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 $patternId): array
    {
        $patternId = absint($patternId);
        $pattern = get_post($patternId);
        if (!$pattern instanceof \WP_Post) {
            return (new UsageResolutionResult([], false, false, true, 0, 0))->toArray();
        }

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