@php
    // Shared Ladill app launcher — IDENTICAL across every app/service.
    // Driven by config/ladill_launcher.php (fully-extracted apps only) + icons
    // from public/images/launcher-icons/. Omits the current app (matched by
    // APP_URL host). To replicate elsewhere, copy this file + the config +
    // the launcher-icons folder verbatim.
    $sidebar = (bool) ($sidebar ?? false);
    $selfHost = parse_url((string) config('app.url'), PHP_URL_HOST);
    $launcherApps = array_values(array_filter(
        config('ladill_launcher.apps', []),
        fn (array $app) => parse_url($app['url'], PHP_URL_HOST) !== $selfHost
    ));
    $launcherOverflows = count($launcherApps) > 15;
@endphp
@if (! empty($launcherApps))
<div class="relative" x-data="{
    appsOpen: false,
    showScrollMore: {{ $launcherOverflows ? 'true' : 'false' }},
    checkScrollEnd() {
        const el = this.$refs.appsScroller;
        if (! el) return;
        this.showScrollMore = el.scrollTop + el.clientHeight < el.scrollHeight - 4;
    },
    toggleApps() {
        this.appsOpen = ! this.appsOpen;
        if (this.appsOpen) {
            this.$nextTick(() => this.checkScrollEnd());
        } else {
            this.showScrollMore = {{ $launcherOverflows ? 'true' : 'false' }};
        }
    },
}" @click.outside="appsOpen = false" @keydown.escape.window="appsOpen = false">
    <button type="button" @click="toggleApps()"
            @class([
                'inline-flex items-center justify-center rounded-xl border border-slate-200 text-slate-600 transition hover:bg-slate-50',
                'p-2' => ! $sidebar,
                'w-full gap-3 px-3 py-2 text-[13px] hover:text-slate-900' => $sidebar,
            ])
            :class="appsOpen ? 'bg-slate-50 text-slate-900' : ''" aria-label="All apps">
        <svg @class(['shrink-0', 'h-5 w-5' => ! $sidebar, 'h-[18px] w-[18px]' => $sidebar]) viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
            <path d="M4.5 0.5H1.5C0.947715 0.5 0.5 0.947715 0.5 1.5V4.5C0.5 5.05228 0.947715 5.5 1.5 5.5H4.5C5.05228 5.5 5.5 5.05228 5.5 4.5V1.5C5.5 0.947715 5.05228 0.5 4.5 0.5Z"/>
            <path d="M12.5 0.5H9.5C8.94772 0.5 8.5 0.947715 8.5 1.5V4.5C8.5 5.05228 8.94772 5.5 9.5 5.5H12.5C13.0523 5.5 13.5 5.05228 13.5 4.5V1.5C13.5 0.947715 13.0523 0.5 12.5 0.5Z"/>
            <path d="M4.5 8.5H1.5C0.947715 8.5 0.5 8.94772 0.5 9.5V12.5C0.5 13.0523 0.947715 13.5 1.5 13.5H4.5C5.05228 13.5 5.5 13.0523 5.5 12.5V9.5C5.5 8.94772 5.05228 8.5 4.5 8.5Z"/>
            <path d="M12.5 8.5H9.5C8.94772 8.5 8.5 8.94772 8.5 9.5V12.5C8.5 13.0523 8.94772 13.5 9.5 13.5H12.5C13.0523 13.5 13.5 13.0523 13.5 12.5V9.5C13.5 8.94772 13.0523 8.5 12.5 8.5Z"/>
        </svg>
        @if ($sidebar)
            <span class="font-medium">All Ladill apps</span>
        @endif
    </button>
    <div x-show="appsOpen" x-cloak x-transition.origin.top.right
         @class([
             'absolute z-50 w-72 rounded-2xl border border-slate-200 bg-white p-3 shadow-xl',
             'right-0 mt-2' => ! $sidebar,
             'bottom-full left-0 mb-2' => $sidebar,
         ])>
        <p class="px-1 pb-2 text-[10px] font-bold uppercase tracking-widest text-slate-400">All apps</p>
        <div @class(['ladill-launcher-apps-wrap' => $launcherOverflows])>
            <div
                class="ladill-launcher-apps"
                @if ($launcherOverflows) x-ref="appsScroller" @scroll="checkScrollEnd()" @endif
            >
                <div class="grid grid-cols-3 gap-1">
                @foreach ($launcherApps as $app)
                    <a href="{{ $app['url'] }}"
                       class="flex min-h-[5.25rem] flex-col items-center justify-center gap-1.5 rounded-xl px-2 py-3 text-center transition hover:bg-indigo-50">
                        <span class="flex h-9 w-9 items-center justify-center rounded-lg bg-slate-100">
                            <img src="{{ asset('images/launcher-icons/'.$app['icon']) }}?v={{ @filemtime(public_path('images/launcher-icons/'.$app['icon'])) ?: '1' }}" alt="" class="h-5 w-5 object-contain" width="20" height="20">
                        </span>
                        <span class="text-[11px] font-medium leading-tight text-slate-700">{{ $app['name'] }}</span>
                    </a>
                @endforeach
                </div>
            </div>
            @if ($launcherOverflows)
                <div class="ladill-launcher-scroll-more" x-show="showScrollMore" x-cloak aria-hidden="true">
                    <svg class="h-3.5 w-3.5" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd"/>
                    </svg>
                </div>
            @endif
        </div>
    </div>
</div>
@endif
