#include "system/dependency_service.h"

#include "core/log.h"
#include "core/process.h"

#include <array>
#include <string>

namespace {

  constexpr Logger kLog("dependencies");

  // Optional CLI tools the shell knows about. Adding a new tracked tool is one line.
  constexpr std::array<const char*, 1> kTrackedTools = {
      "wlsunset",
  };

} // namespace

DependencyService::DependencyService() { rescan(); }

bool DependencyService::has(std::string_view name) const {
  const auto it = m_present.find(std::string(name));
  return it != m_present.end() && it->second;
}

void DependencyService::rescan() {
  m_present.clear();

  std::string missing;
  for (const char* name : kTrackedTools) {
    const bool present = process::commandExists(name);
    m_present.emplace(name, present);
    if (!present) {
      if (!missing.empty()) {
        missing += ", ";
      }
      missing += name;
    }
  }

  if (!missing.empty()) {
    kLog.info("optional CLI tools not found: {}", missing);
  }
}
