<?php

use Core\System\ThemeManager;

$logo   = ThemeManager::getSetting('logo', ['url' => '/themes/matjari/assets/img/logo.png']);
$colors = ThemeManager::getSetting('colors', []);

?>

<?php
// ==============================
// Homepage Fixed Sections Layout
// ==============================

// Index sections by type (ignore order completely)
$sectionsByType = [];

if (!empty($home_sections) && is_array($home_sections)) {
    foreach ($home_sections as $section) {
        if (!empty($section['section_type'])) {
            $sectionsByType[$section['section_type']] = $section;
        }
    }
}

// Helper function to render section safely
function renderHomeSection(string $type, array $sectionsByType): void
{
    if (
        empty($sectionsByType[$type]) ||
        (int)($sectionsByType[$type]['is_enabled'] ?? 0) !== 1
    ) {
        return;
    }

    $section = $sectionsByType[$type];

    $sectionFile = dirname(__DIR__) . '/home_sections/' . $type . '.php';

    if (!file_exists($sectionFile)) {
        return;
    }

    $settings = $section['settings'] ?? [];
    $products = $section['products'] ?? [];

    include $sectionFile;
}

// ==============================
// Fixed Homepage Sections Order
// ==============================

// Hero / Slider
renderHomeSection('hero_slider', $sectionsByType);

// Products Section
renderHomeSection('products_section', $sectionsByType);

// Ads Section
renderHomeSection('ads_section', $sectionsByType);

// FAQ Section
renderHomeSection('faq_section', $sectionsByType);

?>

<script>
document.addEventListener('DOMContentLoaded', function () {

    const reveals = document.querySelectorAll('.reveal');
    if (!reveals.length) return;

    if (!('IntersectionObserver' in window)) {
        reveals.forEach(el => el.classList.add('is-visible'));
        return;
    }

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('is-visible');
                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.15
    });

    reveals.forEach(el => observer.observe(el));
});
</script>

