<?php

declare(strict_types=1);

namespace apexl\Io;

use apexl\Config\ConfigurationInterface;
use apexl\Config\Exceptions\MissingConfigException;
use DI\DependencyException;
use DI\NotFoundException;

/**
 * Get the value of a configuration setting.
 *
 * @param string $key The name of the configuration setting.
 * @param mixed $default The default value to return if the setting is not set.
 * @return mixed The value of the configuration setting or the default value.
 */
function config(string $key, mixed $default = null): mixed
{
    try {
        return container()->get(ConfigurationInterface::class)->get($key);
    } catch (MissingConfigException) {
        return $default;

    } catch (DependencyException|NotFoundException $e) {
        exit($e->getMessage());
    }
}
