<?php

declare(strict_types=1);

use apexl\Config\Configuration;
use apexl\Config\ConfigurationInterface;
use apexl\Io\includes\Utils;
use apexl\Io\interfaces\GlobalDataInterface;
use apexl\Io\interfaces\LazyLoadAllowerInterface;
use apexl\Io\modules\system\interfaces\ModuleFinderInterface;
use apexl\Io\modules\system\services\ConfigModuleFinder;
use apexl\Io\services\FilePaths;
use apexl\Io\services\GlobalData;
use apexl\Io\services\LazyLoadAllowerFromConfigService;
use function DI\autowire;
use function DI\factory;

return [
    ConfigurationInterface::class => factory(function (FilePaths $filePaths): Configuration {
        $config = new Configuration();

        $files = Utils::getFilesInDirectory($filePaths->configDir(), ['php'], ['.local.php', '.example.php']);

        ksort($files, SORT_NATURAL); //ensure we load based on file order, not alpha (in case this matters).
        foreach ($files as $file) {
            $config->load($file);
        }

        return $config;
    }),
    GlobalDataInterface::class => autowire(GlobalData::class),
    LazyLoadAllowerInterface::class => autowire(LazyLoadAllowerFromConfigService::class),
    ModuleFinderInterface::class => autowire(ConfigModuleFinder::class),
];
