@php
    $state = is_array($state ?? null) ? $state : [];

    $surface = array_key_exists('renderExpectations', $state)
        ? $state
        : \App\Filament\Support\NormalizedDiffSurface::build($state, 'unknown');

    $summary = is_array($surface['summary'] ?? null) ? $surface['summary'] : [];
    $availabilityState = is_string($surface['availabilityState'] ?? null) ? (string) $surface['availabilityState'] : 'available';
    $emptyState = is_array($surface['emptyState'] ?? null) ? $surface['emptyState'] : null;
    $hasChanges = ((int) ($summary['added'] ?? 0) + (int) ($summary['removed'] ?? 0) + (int) ($summary['changed'] ?? 0)) > 0;

    if ($emptyState === null && ! $hasChanges && $availabilityState === 'available') {
        $message = is_string($summary['message'] ?? null) && trim((string) $summary['message']) !== ''
            ? trim((string) $summary['message'])
            : 'No normalized changes were found.';

        $emptyState = [
            'title' => 'No normalized changes',
            'message' => $message,
        ];
    }
@endphp

<div
    class="space-y-4"
    data-shared-detail-family="normalized-diff"
    data-shared-normalized-diff-host="{{ $surface['hostKind'] ?? 'unknown' }}"
    data-shared-normalized-diff-state="{{ $availabilityState }}"
>
    @include('filament.infolists.entries.normalized-diff.summary', [
        'surface' => $surface,
    ])

    @if ($emptyState !== null)
        @include('filament.infolists.entries.normalized-diff.empty-state', [
            'emptyState' => $emptyState,
            'availabilityState' => $availabilityState,
        ])
    @endif

    @if ($hasChanges)
        @include('filament.infolists.entries.normalized-diff.groups', [
            'surface' => $surface,
        ])
    @endif
</div>