<?php
declare(strict_types=1);

namespace App\Core\Support;

use DateTimeImmutable;
use Throwable;

final class StringHelper
{
    public static function nullableString(string $value): ?string
    {
        $trimmed = trim($value);
        return $trimmed === '' ? null : $trimmed;
    }

    public static function normalizeDateTime(string $value): ?string
    {
        $trimmed = trim($value);
        if ($trimmed === '') {
            return null;
        }

        try {
            return (new DateTimeImmutable($trimmed))->format('Y-m-d H:i:s');
        } catch (Throwable) {
            return null;
        }
    }

    public static function normalizeColorHex(string $value): string
    {
        $trimmed = trim($value);
        if (preg_match('/^#[0-9a-fA-F]{6}$/', $trimmed) === 1) {
            return strtolower($trimmed);
        }

        return '#64748b';
    }
}
