(function () {
    'use strict';

    const sidebar = document.getElementById('sidebar');
    const backdrop = document.getElementById('sidebar-backdrop');
    const toggle = document.getElementById('sidebarToggle');

    if (toggle && sidebar && backdrop) {
        toggle.addEventListener('click', function () {
            sidebar.classList.toggle('open');
            backdrop.classList.toggle('open');
        });
        backdrop.addEventListener('click', function () {
            sidebar.classList.remove('open');
            backdrop.classList.remove('open');
        });
    }

    document.addEventListener('click', function (event) {
        const btn = event.target.closest('[data-copy]');
        if (!btn) {
            return;
        }

        const targetId = btn.getAttribute('data-copy');
        const input = targetId ? document.getElementById(targetId) : null;
        const text = input ? input.value : (btn.getAttribute('data-copy-text') || '');
        if (!text) {
            return;
        }

        copyToClipboard(text, function () {
            const i18n = window.KARVI_I18N || {};
            const original = btn.innerHTML;
            btn.innerHTML = '<span class="material-symbols-rounded text-lg">check</span> ' + (i18n.copied || 'Copied');
            btn.classList.add('!bg-karvi-950', '!text-white', '!border-karvi-950');
            setTimeout(function () {
                btn.innerHTML = original;
                btn.classList.remove('!bg-karvi-950', '!text-white', '!border-karvi-950');
            }, 2000);
        });
    });

    function copyToClipboard(text, onSuccess) {
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(text).then(onSuccess).catch(function () {
                fallbackCopy(text, onSuccess);
            });
            return;
        }

        fallbackCopy(text, onSuccess);
    }

    function fallbackCopy(text, onSuccess) {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.setAttribute('readonly', '');
        textarea.style.position = 'fixed';
        textarea.style.left = '-9999px';
        document.body.appendChild(textarea);
        textarea.select();

        try {
            if (document.execCommand('copy')) {
                onSuccess();
            }
        } catch (err) {
            // ignore
        }

        document.body.removeChild(textarea);
    }

    document.querySelectorAll('.copy-row-btn').forEach(function (btn) {
        btn.addEventListener('click', function () {
            const url = btn.getAttribute('data-url');
            if (!url) return;
            const original = btn.innerHTML;
            const i18n = window.KARVI_I18N || {};
            copyToClipboard(url, function () {
                btn.innerHTML = '<span class="material-symbols-rounded text-base">check</span> ' + (i18n.copied || 'Copied');
                setTimeout(function () {
                    btn.innerHTML = original;
                }, 1500);
            });
        });
    });
})();
