<?php

namespace GemBoards\Classes;

if ( ! defined('ABSPATH') ) {
	exit;
}

class Config {

	protected static $loaded = [];

	public static function get( $key, $default = null ) {
		$parts = explode('.', $key);
		$file  = $parts[0] ?? null;
		$keys  = array_slice($parts, 1);

		if ( ! $file ) {
return $default;
		}

		if ( ! isset(self::$loaded[ $file ]) ) {
			$path = GEMBOARDS_ROOT_DIR_PATH . 'configs/' . $file . '.php';

			if ( ! file_exists($path) ) {
				return $default;
			}

			self::$loaded[ $file ] = require $path;
		}

		$value = self::$loaded[ $file ];

		foreach ( $keys as $k ) {
			if ( ! is_array($value) || ! array_key_exists($k, $value) ) {
				return $default;
			}
			$value = $value[ $k ];
		}

		return $value;
	}
}
