function toggleSiyak() {
    var div = document.getElementById('siyakDiv');
    if (div.style.height === '0px' || div.style.height === '') {
        div.style.height = '350px';
        div.style.overflowY = 'auto';
        div.style.border = '1px solid #ddd';
        div.style.marginTop = '15px';
        setTimeout(() => {
            var target = document.getElementById('targetAyet');
            if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }, 100);
    } else {
        div.style.height = '0px';
        div.style.border = 'none';
    }
}

function toggleSiyakTevrat() {
    var div = document.getElementById('siyakDivTevrat');
    if (!div) return;
    if (div.style.height === '0px' || div.style.height === '') {
        div.style.height = '350px';
        div.style.overflowY = 'auto';
        div.style.border = '1px solid #ddd';
        div.style.marginTop = '15px';
        setTimeout(() => {
            var target = document.getElementById('targetTevratIncilSoz');
            if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }, 300);
    } else {
        div.style.height = '0px';
        div.style.border = 'none';
        div.style.marginTop = '0px';
    }
}

function toggleSiyakDynamic(divId, targetId) {
    var div = document.getElementById(divId);
    var target = document.getElementById(targetId);
    if (div.style.height === '0px' || div.style.height === '') {
        div.style.height = '300px';
        div.style.overflowY = 'auto';
        div.style.marginTop = '10px';
        div.style.border = '1px solid #cfe2ff';
        setTimeout(() => {
            if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }, 300);
    } else {
        div.style.height = '0px';
        div.style.marginTop = '0px';
        div.style.border = 'none';
    }
}

function toggleSiyakTevratIncilDynamic(divId, targetId) {
    var div = document.getElementById(divId);
    var target = document.getElementById(targetId);
    if (div.style.height === '0px' || div.style.height === '') {
        div.style.height = '300px';
        div.style.overflowY = 'auto';
        div.style.marginTop = '10px';
        div.style.border = '1px solid #cfe2ff';
        setTimeout(() => {
            if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }, 300);
    } else {
        div.style.height = '0px';
        div.style.marginTop = '0px';
        div.style.border = 'none';
    }
}

window.scrollToSelectedAyet = (containerId) => {
    const container = document.getElementById(containerId);
    if (!container) return;
    const selectedAyet = container.querySelector('.secili-ayet');
    if (selectedAyet) {
        selectedAyet.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'nearest' });
    }
};

window.setupLazyLoad = (dotNetHelper) => {
    const observer = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting) {
            dotNetHelper.invokeMethodAsync('LoadNextPage');
        }
    }, { root: null, threshold: 0.1 });
    const watcher = document.getElementById('watcher');
    if (watcher) observer.observe(watcher);
};

window.copyToClipboard = function (text, link) {
    navigator.clipboard.writeText(text)
        .then(function () {
            // Eğer link true ise ilk mesajı, değilse (false ise) ikinci mesajı gösterir.
            if (link) {
                alert("Link kopyalandı!");
            } else {
                alert("Metin kopyalandı!");
            }
        })
        .catch(function (err) {
            console.error('Kopyalama hatası: ', err);
        });
};

window.initDipnotListener = function (dotNetObj) {
    document.addEventListener('click', function (e) {
        var sup = e.target.closest('.ai-dipnot-sup');
        if (sup) {
            e.stopPropagation();
            var mealId = parseInt(sup.getAttribute('data-mealid'));
            var dipNo = parseInt(sup.getAttribute('data-dipno'));
            var metin = sup.getAttribute('data-dipmetin');
            var txt = document.createElement('textarea');
            txt.innerHTML = metin;
            dotNetObj.invokeMethodAsync('DipnotTiklandi', mealId, dipNo, txt.value);
        }
    });
};

window.metinSecimDinle = function (elementId, dotNetObj) {
    var el = document.getElementById(elementId);
    if (!el) return;
    el.addEventListener('mouseup', function () {
        var start = el.selectionStart;
        var end = el.selectionEnd;
        if (start !== end) {
            dotNetObj.invokeMethodAsync('MetinSecildi', el.value.substring(start, end), start, end);
        }
    });
    el.addEventListener('keyup', function () {
        var start = el.selectionStart;
        var end = el.selectionEnd;
        if (start !== end) {
            dotNetObj.invokeMethodAsync('MetinSecildi', el.value.substring(start, end), start, end);
        }
    });
};

window.getSelection = function (elementId) {
    var el = document.getElementById(elementId);
    if (!el) return [0, 0];
    return [el.selectionStart, el.selectionEnd];
};
