<?php

namespace WPSN\Modules\PageBuilders;

defined('ABSPATH') || exit;

/**
 * Shared builder availability detection.
 *
 * We use one helper so Diagnostics and the runtime integrations stay in sync,
 * and to avoid fragile duplicated checks drifting apart over time.
 */
final class BuilderDetector
{
    public static function hasElementor(): bool
    {
        return defined('ELEMENTOR_VERSION') || class_exists('Elementor\\Plugin');
    }

    public static function hasDivi(): bool
    {
        if (defined('ET_BUILDER_VERSION') || defined('ET_CORE_VERSION')) {
            return true;
        }

        if (class_exists('ET_Builder_Plugin') || function_exists('et_pb_is_pagebuilder_used') || function_exists('et_fb_is_enabled')) {
            return true;
        }

        if (function_exists('wp_get_theme')) {
            try {
                $theme = wp_get_theme();
                if (is_object($theme)) {
                    $name = (string) $theme->get('Name');
                    $template = (string) $theme->get_template();
                    $stylesheet = (string) $theme->get_stylesheet();
                    $hay = strtolower($name . ' ' . $template . ' ' . $stylesheet);
                    if (strpos($hay, 'divi') !== false || strpos($hay, 'extra') !== false) {
                        return true;
                    }
                }
            } catch (\Throwable $e) {
                // ignore
            }
        }

        return false;
    }

    public static function hasBeaver(): bool
    {
        return defined('FL_BUILDER_VERSION') || class_exists('FLBuilder') || class_exists('FLBuilderLoader') || class_exists('FLBuilderModel');
    }
}
