<?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 template part entity.
 */
final class TemplatePartUsageResolver
{
    /**
     * Resolve front-end content IDs that reference the given template part entity.
     *
     * @param int $templatePartId Template part 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 $templatePartId): array
    {
        $templatePartId = absint($templatePartId);
        $templatePart = get_post($templatePartId);
        if (!$templatePart instanceof \WP_Post) {
            return (new UsageResolutionResult([], false, false, true, 0, 0))->toArray();
        }

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