<?php

declare(strict_types=1);

namespace apexl\Io\modules\system\services;

use apexl\Config\ConfigurationInterface;
use apexl\Config\Exceptions\MissingConfigException;
use apexl\Io\modules\system\exceptions\ModuleFinderException;
use apexl\Io\modules\system\interfaces\ModuleFinderInterface;

final readonly class ConfigModuleFinder implements ModuleFinderInterface
{
    public function __construct(private ConfigurationInterface $config) {}

    public function installed(): array
    {
        return $this->all();
    }

    public function all(): array
    {
        try {
            return $this->config->get('modules') ?? [];
        } catch (MissingConfigException $e) {
            throw new ModuleFinderException($e->getMessage(), $e->getCode(), $e);
        }
    }
}
