@props([
    'telefones' => [],
])

@php
    $maxGroups = 1;
    $maxNumbers = 4;
    $needsToggle = count($telefones) > $maxGroups;

    if (! $needsToggle) {
        foreach (array_slice($telefones, 0, $maxGroups) as $grupo) {
            if (count($grupo['numeros'] ?? []) > $maxNumbers) {
                $needsToggle = true;
                break;
            }
        }
    }
@endphp

<article class="ma-org-card ma-org-card--contacts">
    <h2 class="ma-org-card__title">{{ __('Contatos') }}</h2>

    <ul class="ma-org-contacts list-unstyled mb-0" data-ma-contacts-list>
        @forelse ($telefones as $groupIndex => $telefone)
            @php
                $hideGroup = $groupIndex >= $maxGroups;
            @endphp
            <li @class([
                'ma-org-contacts__group',
                'd-none' => $hideGroup,
            ]) @if ($hideGroup) data-ma-contacts-collapsed @endif>
                <h3 class="ma-org-contacts__group-title">{{ $telefone['telefone'] }}</h3>
                <ul class="ma-org-contacts__numbers list-unstyled mb-0">
                    @foreach ($telefone['numeros'] as $numIndex => $numero)
                        @php
                            $hideNumber = ! $hideGroup && $numIndex >= $maxNumbers;
                        @endphp
                        <li @class([
                            'ma-org-contacts__number',
                            'd-none' => $hideNumber,
                        ]) @if ($hideNumber) data-ma-contacts-collapsed @endif>
                            <i class="fa-solid fa-phone ma-org-contacts__icon" aria-hidden="true"></i>
                            <a class="ma-org-contacts__link" href="tel:{{ $numero['link'] }}">
                                {{ $numero['numero'] }}
                            </a>
                        </li>
                    @endforeach
                </ul>
            </li>
        @empty
            <li class="ma-org-contacts__empty">{{ __('Nenhum telefone encontrado.') }}</li>
        @endforelse
    </ul>

    @if ($needsToggle)
        <button type="button"
            class="btn btn-light ma-org-contacts__toggle"
            data-ma-contacts-toggle
            data-label-more="{{ __('Ver mais') }}"
            data-label-less="{{ __('Mostrar menos') }}"
            aria-expanded="false">
            {{ __('Ver mais') }}
        </button>
    @endif
</article>
