<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue4112Test extends AbstractFunctional
{
    /**
     * Problem deleting all sheets then adding one.
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('providerSheetNumber')]
    public function testIssue4112(?int $sheetNumber): void
    {
        $mySpreadsheet = new Spreadsheet();
        $mySpreadsheet->removeSheetByIndex(0);
        $worksheet = new Worksheet($mySpreadsheet, 'addedsheet');
        self::assertSame(-1, $mySpreadsheet->getActiveSheetIndex());
        $mySpreadsheet->addSheet($worksheet, $sheetNumber);
        self::assertSame('addedsheet', $mySpreadsheet->getActiveSheet()->getTitle());
        $row = 1;
        $col = 1;
        $worksheet->getCell([$col, $row])->setValue('id_uti');
        self::assertSame('id_uti', $worksheet->getCell([$col, $row])->getValue());
        $mySpreadsheet->disconnectWorksheets();
    }

    public static function providerSheetNumber(): array
    {
        return [
            'problem case' => [0],
            'normal case' => [null],
            'negative 1 (as if there were no sheets)' => [-1],
            'diffeent negative number' => [-4],
            'positive number' => [4],
        ];
    }
}
