<?php

namespace QUI\InfoPopup;

use QUI;
use QUI\Template;

/**
 * Class EventHandler
 *
 * Frontend delivery of the info popups.
 *
 * @package quiqqer/info-popup
 */
class EventHandler
{
    /**
     * onTemplateGetHeader event.
     *
     * Embeds a tiny bootstrap only when the current project has active and
     * time-valid entries. The bootstrap loads the popups asynchronously and
     * performs the user-dependent visibility check via a non-cached ajax call.
     */
    public static function onTemplateGetHeader(Template $Template): void
    {
        try {
            $Project = QUI::getRewrite()->getProject();

            if ($Project === null || !Handler::hasActiveEntries($Project)) {
                return;
            }
        } catch (QUI\Exception) {
            // no project or table not available (e.g. setup not run yet)
            return;
        }

        $Template->extendHeader(
            '<script>
                require(["package/quiqqer/info-popup/bin/Popups"], function (Popups) {
                    Popups.load();
                });
            </script>'
        );
    }
}
