<?php
declare(strict_types=1);

namespace Ekesto\CacheInvalidator\Core;

/**
 * Deterministic emission payload builder for trusted/untrusted URL sets.
 */
final class EmissionBuilder
{
    /**
     * Build one canonical emission payload from trusted and untrusted URL buckets.
     *
     * @param array<int,mixed> $trusted Trusted URL candidates.
     * @param array<int,mixed> $untrusted Untrusted URL candidates.
     * @return array{urls:array<int,string>,trusted_urls:array<int,string>}
     */
    public static function build(array $trusted, array $untrusted): array
    {
        $trusted = self::normalizeUrls($trusted);
        $untrusted = self::normalizeUrls($untrusted);
        $urls = self::normalizeUrls(array_merge($trusted, $untrusted));

        return [
            'urls' => $urls,
            'trusted_urls' => $trusted,
        ];
    }

    /**
     * Normalize URL list (string-cast, trim, dedupe, drop empty values).
     *
     * @param array<int,mixed> $urls Raw URL list.
     * @return array<int,string>
     */
    private static function normalizeUrls(array $urls): array
    {
        return array_values(array_unique(array_filter(array_map(
            static fn($url): string => trim((string) $url),
            $urls
        ))));
    }
}
