<?php

namespace classes;

use interfaces\language_pack_i;

class language_packs
{
    /**
     * The language packs
     * @var array
     */
    protected array $language_packs = [];
    protected string $default_language = 'en_us';

    public function __construct()
    {
        $this->loadLanguagePacks();
    }

    /**
     * Load the language packs
     */
    protected function loadLanguagePacks(): void
    {
        $files = glob('languages/*.php');
        foreach ( $files as $file ) {
            require_once $file;
            $class = 'languages\\' . str_replace('.php', '', basename($file));
            $this->language_packs[] = new $class();
        }
    }

    /**
     * Get the default language pack
     * @return language_pack_i The default language pack
     */
    public function getDefaultLanguagePack(): object
    {
        return $this->getLanguagePack($this->default_language);
    }

    /**
     * Get the language pack for a language
     * @param string $language The language code of the language pack
     * @return language_pack_i The language pack
     */

    public function getLanguagePack(string $language): object
    {
        foreach ( $this->language_packs as $language_pack ) {
            if ($language_pack->getLanguage() === $language) {
                return $language_pack;
            }
        }
        return $this->getLanguagePack($this->default_language);
    }
}