<?php

declare(strict_types=1);

namespace Drupal\native_observability_dashboard\Service;

use Drupal\Core\Extension\ModuleHandlerInterface;

/**
 * Resolves the availability of frontend chart libraries.
 */
final readonly class DashboardLibraryAvailabilityResolver {

  /**
   * Creates a new dashboard library availability resolver instance.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  public function __construct(
    private ModuleHandlerInterface $moduleHandler,
  ) {
  }

  /**
   * Checks whether the Apache ECharts library is available.
   *
   * @return bool
   *   TRUE when the ECharts library is available, FALSE otherwise.
   */
  public function hasEchartsLibrary(): bool {
    $paths = [
      DRUPAL_ROOT . '/libraries/echarts/dist/echarts.min.js',
      DRUPAL_ROOT . '/libraries/echarts/dist/echarts.js',
    ];

    foreach ($paths as $path) {
      if (file_exists($path)) {
        return TRUE;
      }
    }

    return FALSE;
  }

}
