<?php

declare(strict_types=1);

namespace App\Filament\Components\Table;

use Filament\Tables\Columns\TextColumn;

class DefaultPriceTextColumn extends TextColumn
{
    public static function create(): static
    {
        return parent::make('default_price')
            ->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) {
                    // Format the number with separators
                    return number_format(
                        (float) $price,
                        0,
                        '/',
                        '،'
                    ) . ' تومان';
                }, $state);

                return implode(' - ', $formattedPrices);
            })
            ->toggleable();
    }
}
