<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementWriteOffs\Pages;

use App\Filament\Resources\DberpSettlementWriteOffs\DberpSettlementWriteOffResource;
use App\Support\DberpSettlementWriteOffCounterpartyNames;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Pagination\AbstractPaginator;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;

class ListDberpSettlementWriteOffs extends ListRecords
{
    protected static string $resource = DberpSettlementWriteOffResource::class;

    public function render(): View
    {
        $records = $this->getTableRecords();
        $items = match (true) {
            $records instanceof AbstractPaginator, $records instanceof CursorPaginator => collect($records->items()),
            $records instanceof Collection => $records,
            default => collect(),
        };

        if ($items->isNotEmpty()) {
            app(DberpSettlementWriteOffCounterpartyNames::class)->preload($items);
        }

        return parent::render();
    }

    protected function getHeaderActions(): array
    {
        return [
            CreateAction::make(),
        ];
    }
}
