<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;

class ChiInvLeftTailTest extends AllSetupTeardown
{
    #[\PHPUnit\Framework\Attributes\DataProvider('providerCHIINV')]
    public function testCHIINV(mixed $expectedResult, mixed ...$args): void
    {
        $this->runTestCaseReference('CHISQ.INV', $expectedResult, ...$args);
    }

    public static function providerCHIINV(): array
    {
        return require 'tests/data/Calculation/Statistical/CHIINVLeftTail.php';
    }

    public function invVersusDistTest(): void
    {
        $expectedResult = 4.671330448981;
        $probability = 0.3;
        $degrees = 7;
        $calculation = Calculation::getInstance();
        $formula = "=CHISQ.INV($probability, $degrees)";
        /** @var float|int|string */
        $result = $calculation->calculateFormula($formula);
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-8);
        $formula = "=CHISQ.DIST($result, $degrees)";
        $result = $calculation->calculateFormula($formula);
        self::assertEqualsWithDelta($probability, $result, 1.0e-8);
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('providerChiInvLeftTailArray')]
    public function testChiInvLeftTailArray(array $expectedResult, string $probabilities, string $degrees): void
    {
        $calculation = Calculation::getInstance();

        $formula = "=CHISQ.INV({$probabilities}, {$degrees})";
        $result = $calculation->calculateFormula($formula);
        self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
    }

    public static function providerChiInvLeftTailArray(): array
    {
        return [
            'row/column vectors' => [
                [
                    [5.0816470296362795, 6.345811195521517, 1.508898337422818],
                    [9.61151737996991, 11.34032237742413, 4.0773397083341045],
                ],
                '{0.35, 0.5, 0.018}',
                '{7; 12}',
            ],
        ];
    }
}
