<?php
declare(strict_types=1);

namespace ProRank\SEO\Integrations\WPBakery;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * WPBakery Page Builder integration marker.
 *
 * WPBakery (js_composer) runs the Backend Editor on the standard WordPress
 * post edit screen, so the existing meta box renders without any extra
 * mount work. The Frontend Editor (vc_action=vc_inline / vc_editable=true)
 * is a separate UI that we deliberately do NOT inject the floating SEO
 * panel into until its enqueue hooks are source-confirmed; rendering
 * untested chrome inside the inline editor can break WPBakery's own UX.
 *
 * Read-only responsibilities active today:
 *   - Mark WPBakery as detected for compatibility logic.
 *   - Expose helpers other modules can call to know if the current request
 *     is the WPBakery Frontend Editor (so they can avoid optimization).
 *
 * This class intentionally adds no scripts/styles in v1.
 */
class WPBakeryIntegration {

    private static ?self $instance = null;

    public static function get_instance(): self {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        if (!self::is_wpbakery_active()) {
            return;
        }
    }

    /**
     * WPBakery is loaded when any of its public surface is present.
     */
    public static function is_wpbakery_active(): bool {
        return defined('WPB_VC_VERSION')
            || class_exists('Vc_Manager')
            || function_exists('vc_map');
    }

    /**
     * Detect WPBakery Frontend Editor requests.
     *
     * WPBakery's inline editor sets vc_action=vc_inline on the request URL,
     * and once mounted it also exposes vc_editable=true. We treat either
     * signal as the frontend editor.
     */
    public static function is_frontend_editor_request(): bool {
        // phpcs:disable WordPress.Security.NonceVerification.Recommended
        if (isset($_GET['vc_action']) && $_GET['vc_action'] === 'vc_inline') {
            return true;
        }
        if (isset($_GET['vc_editable']) && $_GET['vc_editable'] === 'true') {
            return true;
        }
        // phpcs:enable
        return false;
    }
}
