<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\Messenger;

use App\Shared\Domain\Tenant\TenantId as DomainTenantId;

use function get_object_vars;
use function is_string;
use function trim;

final readonly class TenantMessageTenantIdResolver
{
    public function resolve(object $message): ?string
    {
        $vars = get_object_vars($message);
        if (!isset($vars['tenantId'])) {
            return null;
        }

        $tenantId = $vars['tenantId'];

        if ($tenantId instanceof DomainTenantId) {
            return (string) $tenantId;
        }

        if (!is_string($tenantId)) {
            return null;
        }

        $tenantId = trim($tenantId);

        return $tenantId !== '' ? $tenantId : null;
    }
}
