<?php

namespace App\Support;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class Branding
{
    public static function assetUrl(mixed $path): ?string
    {
        if (is_array($path)) {
            $path = array_values(array_filter($path))[0] ?? null;
        }

        $path = trim((string) $path);

        if ($path === '') {
            return null;
        }

        if (Str::startsWith($path, ['http://', 'https://', 'data:', '/'])) {
            return $path;
        }

        return Storage::disk('public')->url($path);
    }

    public static function initials(?string $name): string
    {
        $name = trim((string) $name);

        if ($name === '') {
            return 'RM';
        }

        $words = preg_split('/\s+/', $name) ?: [];
        $initials = collect($words)
            ->map(fn (string $word): string => Str::upper(Str::substr($word, 0, 1)))
            ->take(2)
            ->implode('');

        return $initials !== '' ? $initials : 'RM';
    }
}
