<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

namespace Tassos\CodeSnippets\Administrator\Location;

defined('_JEXEC') or die;

/**
 * Page Load Location (PHP)
 * 
 * Executes PHP snippets on every page load.
 * Runs immediately during initialization (no event registration).
 */
class Pageload extends AbstractLocation
{
    /**
     * No event needed - runs immediately
     *
     * @return string|null
     */
    public function getEvent(): ?string
    {
        return null;
    }

    /**
     * Check if this location can run
     *
     * @return bool
     */
    public function canRun(): bool
    {
        return $this->isSite();
    }

    /**
     * Execute PHP snippets immediately
     *
     * @return void
     */
    public function inject(): void
    {
        foreach ($this->snippets as $snippet)
        {
            $assetKey = 'tcs_snippet_' . $snippet->id;

            if (isset(self::$registeredAssets[$assetKey]))
            {
                continue;
            }

            // Run the snippet (output is not used for pageload - it's just executed)
            $this->runSnippet($snippet);

            self::$registeredAssets[$assetKey] = true;
        }
    }
}
