
<?php
    $sourceMap = \App\Services\Site\Templates\VoyageTemplate::dataSources();

    // "tours" işareti acentenin web'de listelenebilen tur tiplerine açılır:
    // yalnızca yetkisi olan tipler bağlantı olarak çıkar (yoksa üçü de).
    $tourScreens = [
        'kultur-turu' => ['Kültür turları', 'agency.products.kultur-turlari.index'],
        'yurtdisi-turu' => ['Yurt dışı turları', 'agency.products.yurtdisi-turlari.index'],
        'gunubirlik-tur' => ['Günübirlik turlar', 'agency.products.gunubirlik-turlar.index'],
    ];

    $activeCodes = app(\App\Services\Site\SiteService::class)
        ->activeProductTypeCodes(auth()->user()->agency);

    $tourLinks = collect($tourScreens)
        ->filter(fn ($screen, $code) => in_array($code, $activeCodes, true))
        ->whenEmpty(fn () => collect($tourScreens))
        ->map(fn ($screen) => ['label' => $screen[0], 'route' => $screen[1]])
        ->values()
        ->all();

    $resolveLinks = function ($links) use ($tourLinks) {
        if ($links === 'tours') {
            $links = $tourLinks;
        }

        return collect($links ?? [])
            // Route adı tanımlı değilse (modül kapalıysa) bağlantı çizilmez
            ->filter(fn ($link) => \Illuminate\Support\Facades\Route::has($link['route']))
            ->map(fn ($link) => ['label' => $link['label'], 'url' => route($link['route'])])
            ->values()
            ->all();
    };

    $sourcePayload = collect($availableSections)->map(fn ($definition, $key) => [
        'fields' => collect($definition['fields'] ?? [])->pluck('label')->filter()->values()->all(),
        'blocks' => collect($sourceMap[$key] ?? [])->map(fn ($block) => [
            'icon' => $block['icon'],
            'title' => $block['title'],
            'text' => $block['text'],
            'links' => $resolveLinks($block['links'] ?? null),
        ])->all(),
    ])->all();
?>

<script>
(function () {
    'use strict';

    const SOURCES = <?php echo json_encode($sourcePayload, 15, 512) ?>;
    const escape = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));

    /** Bölüm tipinin "veriyi nereden çeker" panelini HTML olarak üretir. */
    window.sxSourceHtml = function (type) {
        const source = SOURCES[type];
        if (!source) return '';

        let html = '';

        if (source.fields.length) {
            html += '<div class="sx-src__row">' +
                '<span class="sx-src__ic"><i class="fas fa-sliders"></i></span>' +
                '<span class="sx-src__b"><b>Editörde doldurduğunuz alanlar</b>' +
                '<span class="sx-src__chips">' +
                source.fields.map((field) => '<span class="sx-tag">' + escape(field) + '</span>').join('') +
                '</span></span></div>';
        }

        source.blocks.forEach((block) => {
            html += '<div class="sx-src__row">' +
                '<span class="sx-src__ic"><i class="fas ' + escape(block.icon) + '"></i></span>' +
                '<span class="sx-src__b"><b>' + escape(block.title) + '</b>' +
                '<p>' + escape(block.text) + '</p>' +
                (block.links.length
                    ? '<span class="sx-src__chips">' + block.links.map((link) =>
                        '<a class="sx-chip" href="' + escape(link.url) + '" target="_blank" rel="noopener">' +
                        '<i class="fas fa-arrow-up-right-from-square"></i> ' + escape(link.label) + '</a>').join('') +
                      '</span>'
                    : '') +
                '</span></div>';
        });

        return html;
    };
})();
</script>
<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/site/partials/_section-sources.blade.php ENDPATH**/ ?>