// Laos Coffee — Splash + video + reveal + ön yüz koruma (admin hariç)
(function () {
    'use strict';

    function blockInspect(e) {
        var k = e.keyCode || e.which;
        var ctrl = e.ctrlKey || e.metaKey;
        if (k === 123) { e.preventDefault(); return false; }
        if (ctrl && e.shiftKey && (k === 73 || k === 74 || k === 67)) { e.preventDefault(); return false; }
        if (ctrl && !e.shiftKey && k === 85) { e.preventDefault(); return false; }
        if (ctrl && k === 83) { e.preventDefault(); return false; }
        return true;
    }
    document.addEventListener('contextmenu', function (e) { e.preventDefault(); }, false);
    document.addEventListener('keydown', blockInspect, false);
    document.addEventListener('dragstart', function (e) {
        if (e.target && (e.target.tagName === 'IMG' || e.target.tagName === 'VIDEO')) {
            e.preventDefault();
        }
    }, false);

    const splash = document.getElementById('splash');
    const menuBtn = document.getElementById('splashMenuBtn');
    const phoneScreen = document.getElementById('phoneScreen');
    function isDesktopPhone() {
        return window.matchMedia('(min-width: 1024px)').matches;
    }
    function lockScroll() {
        if (isDesktopPhone() && phoneScreen) {
            phoneScreen.style.overflow = 'hidden';
        } else {
            document.body.style.overflow = 'hidden';
        }
    }
    function unlockScroll() {
        if (phoneScreen) phoneScreen.style.overflow = '';
        document.body.style.overflow = '';
    }
    function closeSplash() {
        if (!splash) return;
        splash.classList.add('hide');
        unlockScroll();
        try { sessionStorage.setItem('laos_splash', '1'); } catch (e) {}
        setTimeout(function () {
            var main = document.getElementById('mainPage') || document.getElementById('categories');
            if (main) {
                if (isDesktopPhone() && phoneScreen) {
                    phoneScreen.scrollTo({ top: main.offsetTop - 8, behavior: 'smooth' });
                } else {
                    main.scrollIntoView({ behavior: 'smooth' });
                }
            }
        }, 400);
    }
    if (splash) {
        var seen = false;
        try { seen = sessionStorage.getItem('laos_splash') === '1'; } catch (e) {}
        if (seen) {
            splash.classList.add('hide');
        } else {
            lockScroll();
        }
        if (menuBtn) menuBtn.addEventListener('click', closeSplash);
    }

    var reveals = document.querySelectorAll('.reveal');
    if ('IntersectionObserver' in window && reveals.length) {
        var io = new IntersectionObserver(function (entries) {
            entries.forEach(function (en) {
                if (en.isIntersecting) {
                    en.target.classList.add('in');
                    io.unobserve(en.target);
                }
            });
        }, { threshold: 0.1, rootMargin: '0px 0px -30px 0px' });
        reveals.forEach(function (el) { io.observe(el); });
    } else {
        reveals.forEach(function (el) { el.classList.add('in'); });
    }

    var navItems = document.querySelectorAll('.cat-nav-item');
    var sections = document.querySelectorAll('.menu-cat');
    if (navItems.length && sections.length) {
        navItems.forEach(function (a) {
            a.addEventListener('click', function (e) {
                var id = a.getAttribute('href');
                if (id && id.charAt(0) === '#') {
                    var t = document.querySelector(id);
                    if (t) { e.preventDefault(); t.scrollIntoView({ behavior: 'smooth' }); }
                }
            });
        });
        var so = new IntersectionObserver(function (entries) {
            entries.forEach(function (en) {
                if (en.isIntersecting) {
                    var id = en.target.id;
                    navItems.forEach(function (a) {
                        a.classList.toggle('active', a.getAttribute('href') === '#' + id);
                    });
                }
            });
        }, { rootMargin: '-40% 0px -55% 0px' });
        sections.forEach(function (s) { so.observe(s); });
    }

    function bootVideo(v) {
        v.setAttribute('playsinline', '');
        v.setAttribute('webkit-playsinline', '');
        if (!v.hasAttribute('muted')) v.muted = true;
        v.addEventListener('error', function () {
            var p = v.getAttribute('poster');
            if (p && v.parentElement) {
                var img = document.createElement('img');
                img.src = p;
                img.alt = '';
                img.className = v.className;
                img.style.cssText = 'width:100%;height:100%;object-fit:contain;object-position:center';
                v.replaceWith(img);
            }
        });
    }
    document.querySelectorAll('video').forEach(bootVideo);

    var lazyVids = document.querySelectorAll('video[preload="metadata"], video[preload="none"]');
    if ('IntersectionObserver' in window && lazyVids.length) {
        var vo = new IntersectionObserver(function (entries) {
            entries.forEach(function (en) {
                var v = en.target;
                if (en.isIntersecting) {
                    if (v.preload === 'none') v.preload = 'metadata';
                    v.play().catch(function () {});
                } else {
                    v.pause();
                }
            });
        }, { threshold: 0.15, root: document.getElementById('phoneScreen') || null });
        lazyVids.forEach(function (v) { vo.observe(v); });
    }

    var listVids = document.querySelectorAll('.pr-media video, .laos-cat-media video, .laos-feat-media video');
    if ('IntersectionObserver' in window && listVids.length) {
        var lo = new IntersectionObserver(function (entries) {
            entries.forEach(function (en) {
                var v = en.target;
                if (en.isIntersecting) {
                    v.play().catch(function () { setTimeout(function () { v.play().catch(function () {}); }, 200); });
                } else {
                    v.pause();
                }
            });
        }, { threshold: 0.2, root: document.getElementById('phoneScreen') || null });
        listVids.forEach(function (v) { lo.observe(v); });
    }
})();
