<?php

declare(strict_types=1);

namespace App\Filament\Components\Table;

use Filament\Tables\Columns\TextColumn;

class PricesTextColumn extends TextColumn
{
    public static function create(): static
    {
        return parent::make('prices')
            ->translateLabel()
            ->sortable()
            ->formatStateUsing(function ($state, $record) {
                if (! is_array($state)) {
                    // Convert the state to an array if it's not already
                    $state = explode(',', str_replace(' ', '', $state));
                    $state = array_map('intval', $state); // Convert elements to integers
                }
                $formattedPrices = array_map(function ($price) {
                    // First get the formatted number with separators
                    $formatted = number_format(
                        (float) $price,
                        0,
                        '/',
                        '،'
                    );

                    // Then convert to Persian numbers
                    return str_replace(
                        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                        ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'],
                        $formatted
                    ) . ' تومان';
                }, $state);

                return '<div style="text-align: left;">' . implode('<br>', $formattedPrices) . '</div>';
            })
            ->html() // Allow HTML line breaks
            ->toggleable();
    }
}
