<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Mercure;


use CpCreation\VitiCore\Bridge\Symfony\Bundle\Context\UserContext;

abstract readonly class ChannelGenerator
{
    /**
     * @return list<string>
     */
    public static function privateWindowIdTopic(string $canonical): array
    {
        if (!UserContext::$identifierWindowId) {
            return [$canonical];
        }

        return [$canonical, strtr(self::subscriptionRoute(UserContext::$identifierWindowId), [
            '{?topic}' => '?topic='.urlencode($canonical),
        ])];
    }

    /**
     * @return list<string>
     */
    public static function privateSessionTopic(string $canonical): array
    {
        if (!UserContext::$identifierSession) {
            return [$canonical];
        }

        return [$canonical, strtr(self::subscriptionRoute(UserContext::$identifierSession), [
            '{?topic}' => '?topic='.urlencode($canonical),
        ])];
    }

    /**
     * @return list<string>
     */
    public static function privateUserIdTopic(string $canonical): array
    {
        if (!UserContext::$identifierUserId) {
            return [$canonical];
        }

        return [$canonical, strtr(self::subscriptionRoute(UserContext::$identifierUserId), [
            '{?topic}' => '?topic='.urlencode($canonical),
        ])];
    }

    public static function subscriptionRoute(string $context): string
    {
        return \sprintf('/users/%s/{?topic}', $context);
    }
}
