<?php

if (!function_exists('format_money')) {
    function format_money(int|float|null $piasters, string $currency = 'ج.م'): string
    {
        if ($piasters === null) {
            $piasters = 0;
        }

        return number_format($piasters / 100, 2) . ' ' . $currency;
    }
}

if (!function_exists('money_parts')) {
    /**
     * The same conversion, split so a template can typeset the two halves apart.
     *
     * A headline amount wants the figure large and the currency small beside it;
     * concatenated into one string the only way to do that is to divide by 100
     * in a view, and the division belongs here and nowhere else.
     *
     * @return array{amount: string, currency: string}
     */
    function money_parts(int|float|null $piasters, string $currency = 'ج.م'): array
    {
        return [
            'amount' => number_format(($piasters ?? 0) / 100, 2),
            'currency' => $currency,
        ];
    }
}
