(function ($, Drupal, once, drupalSettings) {
  Drupal.behaviors.apiMonitorSwagger = {
    attach: function (context, settings) {
      const elements = once('apiMonitorSwagger', '#swagger-ui', context);

      elements.forEach(function (el) {
        // Recuperiamo i dati dai settings passati dal Controller
        const providerId = drupalSettings.api_monitor_client.current_provider_id;
        const apiId = drupalSettings.api_monitor_client.current_api_id;

        const ui = SwaggerUIBundle({
          url: `/admin/api-monitor/swagger/${providerId}/${apiId}/openapi.json`,
          dom_id: '#swagger-ui',
          presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
          ],
          layout: "StandaloneLayout",
        });
      });
    }
  };
})(jQuery, Drupal, once, drupalSettings);
