<?php
/**
 * SiteGround Optimizer
 *
 * @package SAAI\Third_Party
 */

namespace SAAI\Third_Party;

use SAAI\Traits\Singleton_Trait;
use SAAI\Helpers\Helper;
use SAAI\Factories\Vite_App;

/**
 * SiteGround Optimizer
 */
class SiteGround_Optimizer {

    const PLUGIN_BASENAME = 'sg-cachepress/sg-cachepress.php';

    use Singleton_Trait;

    /**
     * Holds the class instance object.
     *
     * @since 1.0.7
     * @var SiteGround_Optimizer $instance
     */
    protected static $instance;

    /**
     * Run the application.
     *
     * @since 1.0.7
     * @access public
     */
    public function run() {
        if ( ! is_plugin_active( self::PLUGIN_BASENAME ) ) {
            return;
        }

        add_filter( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts_in_sg_optimizer' ) );
    }

    /**
     * Enqueue frontend scripts in SiteGround Optimizer.
     *
     * @since 1.0.7
     * @access public
     */
    public function enqueue_frontend_scripts_in_sg_optimizer() {
        if ( strpos( $_SERVER['REQUEST_URI'], 'siteground-optimizer' ) === false ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
            return;
        }

        // Enqueue the product questions script so it will show up in the SiteGround Optimizer exclude JS list.
        $app = new Vite_App(
            'product-questions-script',
            'src/vanilla/single/product/product-question.ts',
            array( 'jquery', 'wp-i18n' ),
            array(),
            'saaiPqObj',
            array( 'storeagent-fonts' )
        );
        $app->enqueue();
    }
}
