<?php

namespace App\Support;

class PersianDigits
{
    private const FA = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

    private const FROM = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

    private const TO = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

    public static function toEnglish(?string $value): ?string
    {
        if ($value === null || $value === '') {
            return $value;
        }

        return str_replace(self::FROM, self::TO, $value);
    }

    public static function toPersian(int|float|string|null $value): string
    {
        if ($value === null || $value === '') {
            return '';
        }

        return str_replace(range(0, 9), self::FA, (string) $value);
    }

    public static function format(int|float|null $number, int $decimals = 0): string
    {
        if ($number === null) {
            return '';
        }

        return self::toPersian(number_format($number, $decimals));
    }
}
