<?php
declare(strict_types=1);
/**
 * Renders a one-time flash queued by Security::Flash() / Security::Deny() as a
 * SweetAlert — so permission problems greet the user with a friendly dialog on
 * a working page, instead of a bare "403 Forbidden" error page.
 *
 * Include once, near the end of <body>, on any page that can be a redirect
 * target. Requires SweetAlert2 to already be loaded.
 */
$ppFlash = $_SESSION['pp_flash'] ?? null;
unset($_SESSION['pp_flash']);
if (!is_array($ppFlash)) {
    return;
}
?>
<script>
    (function () {
        var flash = <?= json_encode([
                        'type'  => (string) ($ppFlash['type'] ?? 'info'),
                        'title' => (string) ($ppFlash['title'] ?? ''),
                        'text'  => (string) ($ppFlash['text'] ?? ''),
                    ], JSON_UNESCAPED_SLASHES) ?>;
        function show() {
            if (typeof Swal === 'undefined') return;
            Swal.fire({ icon: flash.type, title: flash.title, text: flash.text, confirmButtonText: 'Got it' });
        }
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', show, { once: true });
        } else {
            show();
        }
    }());
</script>
