Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.DataBinding

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only proof that PivotTable export ownership stops at deterministic
    ''' snapshot-to-text writer policy. It does not open file, Excel, clipboard,
    ''' PDF, storage, or provider paths.
    ''' </summary>
    Friend NotInheritable Class MASPivotTableExportWriterPolicyGate
        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Try
                If Not MASPivotTablePolicy.HasExportWriterPolicyPath() Then Return False

                Dim pivot As MASPivotTable = MASPivotTable.Create("Export writer gate")
                pivot.AddColumn("Q1", "Q1")
                pivot.AddColumn("Q2", "Q2")
                pivot.AddRow("North", "North")
                pivot.AddRow("South, East", "South, East")
                pivot.SetCellValue("North", "Q1", 10D)
                pivot.SetCellValue("North", "Q2", 20D)
                pivot.SetCellValue("South, East", "Q1", 5D)
                pivot.SetCellValue("South, East", "Q2", 15D)

                Dim snapshot As MASPivotExportSnapshot = pivot.CreatePivotExportSnapshot()
                Dim text As String = MASPivotDelimitedExportWriter.CreateDelimitedText(snapshot, MASPivotDelimitedExportOptions.CreateDefault())

                If String.IsNullOrWhiteSpace(text) Then Return False
                If Not text.Contains("Row Total") Then Return False
                If Not text.Contains("Column Total") Then Return False
                If Not text.Contains("North,10,20,30") Then Return False
                If Not text.Contains(ChrW(34) & "South, East" & ChrW(34) & ",5,15,20") Then Return False
                If Not text.Contains("Column Total,15,35,50") Then Return False

                Dim semicolonText As String = MASPivotDelimitedExportWriter.CreateDelimitedText(snapshot, New MASPivotDelimitedExportOptions(";"c, True, True))
                If Not semicolonText.Contains("North;10;20;30") Then Return False
                If semicolonText.Contains(ChrW(34) & "South, East" & ChrW(34)) Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASPivotTableExportWriterPolicyGate")
                Return False
            End Try
        End Function
    End Class

End Namespace
