<?php

namespace Calio\Core;

use Calio\Support\Settings;
use Calio\Support\ProAccess;
use Calio\Support\WidgetCatalog;

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

/**
 * Registers only enabled widgets so disabled modules never reach Elementor.
 */
class ModuleManager
{
    public function __construct()
    {
        add_action('elementor/widgets/register', [$this, 'register_widgets']);
    }

    /**
     * Loads widget classes from the catalog after checking settings and class availability.
     *
     * @param object $widgetsManager Elementor widgets manager instance.
     */
    public function register_widgets($widgetsManager): void
    {
        foreach (WidgetCatalog::all() as $slug => $widget) {
            if (ProAccess::is_widget_locked($widget)) {
                continue;
            }

            if (!Settings::is_widget_enabled($slug)) {
                continue;
            }

            $className = $widget['class'];
            if (!class_exists($className)) {
                continue;
            }

            $widgetsManager->register(new $className());
        }
    }
}
