<?php
/**
 * GT BOGO Admin Protection — Devtools detection + right-click/copy guard.
 * Template loaded by GT_BOGO_Protection::inject_protection_js()
 * Variables: $nonce (wp_json_encode'd), $ajax (wp_json_encode'd)
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<script>
(function() {
    var _sec = { dt: 0, nonce: <?php echo wp_json_encode( $nonce ); ?>, ajax: <?php echo wp_json_encode( $ajax ); ?> };

    document.addEventListener('contextmenu', function(e) {
        if (e.target.closest('.gt-admin-wrap')) { e.preventDefault(); return false; }
    });

    document.addEventListener('keydown', function(e) {
        if (!e.target.closest('.gt-admin-wrap')) return;
        var blocked = (
            (e.ctrlKey && e.key === 'u') ||
            (e.ctrlKey && e.shiftKey && (e.key === 'I' || e.key === 'i' || e.key === 'J' || e.key === 'j')) ||
            (e.key === 'F12')
        );
        if (blocked) { e.preventDefault(); return false; }
    });

    var _dtCheck = function() {
        var open = (window.outerHeight - window.innerHeight > 200) ||
                   (window.outerWidth  - window.innerWidth  > 200);
        if (open && !_sec.dt) {
            _sec.dt++;
            var fd = new FormData();
            fd.append('action', 'gtbogo_log_devtools');
            fd.append('nonce',  _sec.nonce);
            fd.append('count',  _sec.dt);
            fetch(_sec.ajax, {method:'POST', body:fd, credentials:'same-origin'});
            if (_sec.dt >= 5) {
                document.querySelectorAll('.gt-code-sensitive').forEach(function(el) {
                    el.style.filter = 'blur(3px)';
                    el.style.userSelect = 'none';
                });
            }
        } else if (!open) {
            _sec.dt = Math.max(0, _sec.dt - 1);
        }
    };
    setInterval(_dtCheck, 2000);

    document.addEventListener('copy', function(e) {
        var sel = window.getSelection();
        if (!sel) return;
        var node = sel.anchorNode;
        if (node && node.parentElement && node.parentElement.closest('.gt-no-copy')) {
            e.clipboardData.setData('text/plain', '[Protected]');
            e.preventDefault();
        }
    });
})();
</script>
