@php
    $height = ['sm' => 'h-64', 'md' => 'h-96', 'lg' => 'h-[32rem]'][$block->get('height', 'md')] ?? 'h-96';
    $locations = $block->get('use_branch_data', true)
        ? $items->map(fn ($b) => ['name' => $b->name_ar ?? $b->name ?? '', 'address' => $b->address ?? '', 'coords' => $b->coordinates ?? null])
        : collect($block->get('locations') ?: []);
    $primary = $locations->first();

    // A map field stores coordinates as ['lat' => …, 'lng' => …]; Google's embed
    // wants "lat,lng". Passing the array straight through broke the whole block.
    $coords = data_get($primary, 'coords');
    if (is_array($coords)) {
        $lat = data_get($coords, 'lat');
        $lng = data_get($coords, 'lng');
        $coords = (filled($lat) && filled($lng)) ? $lat.','.$lng : null;
    }

    $query = $primary ? ($coords ?: website_text(data_get($primary, 'address'))) : null;
    $query = is_string($query) ? $query : null;
@endphp
<div class="{{ $variant === 'full_width' ? '' : 'max-w-7xl mx-auto px-4 sm:px-6 lg:px-8' }}">
    @if ($block->get('title'))
        <h2 class="ec-heading text-3xl font-bold text-center mb-8">{{ $block->get('title') }}</h2>
    @endif
    <div class="{{ $variant === 'split' ? 'grid md:grid-cols-[20rem_1fr] gap-8' : '' }}">
        @if ($variant === 'split')
            <ul class="flex flex-col gap-3">
                @foreach ($locations as $loc)
                    <li class="ec-surface rounded-xl p-4">
                        <p class="font-semibold">{{ website_text(data_get($loc, 'name')) }}</p>
                        <p class="ec-muted text-sm">{{ website_text(data_get($loc, 'address')) }}</p>
                    </li>
                @endforeach
            </ul>
        @endif
        <div class="{{ $height }} w-full overflow-hidden {{ $variant === 'full_width' ? '' : 'rounded-2xl' }}">
            @if ($query)
                <iframe class="w-full h-full border-0" loading="lazy" allowfullscreen
                        referrerpolicy="no-referrer-when-downgrade"
                        title="{{ $block->get('title') ?: __('الموقع على الخريطة') }}"
                        src="https://www.google.com/maps?q={{ urlencode($query) }}&z={{ (int) $block->get('zoom', 13) }}&output=embed"></iframe>
            @else
                <div class="w-full h-full grid place-items-center ec-surface ec-muted">{{ __('لم يتم تحديد موقع') }}</div>
            @endif
        </div>
    </div>
</div>
