using Xunit;

using Radzen.Documents.Spreadsheet;
namespace Radzen.Blazor.Spreadsheet.Tests;

public class MaxAllFunctionTests
{
    readonly Worksheet sheet = new(10, 10);

    [Fact]
    public void ShouldEvaluateLogicalValuesInRange()
    {
        sheet.Cells["A1"].Value = true;   // 1
        sheet.Cells["A2"].Value = false;  // 0
        sheet.Cells["A3"].Value = 5;      // 5

        sheet.Cells["B1"].Formula = "=MAXA(A1:A3)";

        Assert.Equal(5d, sheet.Cells["B1"].Value);
    }

    [Fact]
    public void ShouldTreatTextInRangeAsZeroAndNumericTextAsNumber()
    {
        sheet.Cells["A1"].Value = "abc"; // -> 0
        sheet.Cells["A2"].Value = "15";  // -> 15
        sheet.Cells["A3"].Value = 10;

        sheet.Cells["B1"].Formula = "=MAXA(A1:A3)";

        Assert.Equal(15d, sheet.Cells["B1"].Value);
    }

    [Fact]
    public void ShouldCountDirectLogicalAndTextArguments()
    {
        sheet.Cells["A1"].Formula = "=MAXA(1=1, \"7\", 1=2)"; // TRUE, "7", FALSE -> 7
        Assert.Equal(7d, sheet.Cells["A1"].Value);
    }

    [Fact]
    public void ShouldReturnZeroWhenNoValues()
    {
        sheet.Cells["A1"].Value = null; // empty
        sheet.Cells["A2"].Value = "";  // empty string -> Empty

        sheet.Cells["B1"].Formula = "=MAXA(A1:A2)";

        Assert.Equal(0d, sheet.Cells["B1"].Value);
    }

    [Fact]
    public void ShouldPropagateErrors()
    {
        sheet.Cells["A1"].Value = 10;
        sheet.Cells["A2"].Value = 0;
        sheet.Cells["A3"].Formula = "=A1/A2"; // #DIV/0!

        sheet.Cells["B1"].Formula = "=MAXA(A1:A3)";

        Assert.Equal(CellError.Div0, sheet.Cells["B1"].Value);
    }
}


