<?php


namespace App\Twig;


use App\Service\KeyLighterVersioner;
use Kadet\Highlighter\KeyLighter;
use Tightenco\Collect\Support\Collection;

class KeyLighterTwigAccess
{
    private $keylighter;
    private $versioner;

    public function __construct(KeyLighter $keylighter, KeyLighterVersioner $versioner)
    {
        $this->keylighter = $keylighter;
        $this->versioner  = $versioner;
    }

    public function getLanguages()
    {
        switch (true) {
            case version_compare(KeyLighter::VERSION, '0.9.0', '>'):
                $languages = require $this->versioner->getCurrentDir().'/Config/metadata.php';
                $languages = collect($languages);

                $languages = $languages->reduce(function (Collection $collection, $language) {
                    return $collection->merge(array_fill_keys($language['name'], [
                        'class'      => $language[0],
                        'standalone' => $language['standalone'],
                        'embeddable' => $language['injectable']
                    ]));
                }, collect());

                return $languages;

            default:
                $languages = collect($this->keylighter->registeredLanguages('name', true));

                return $languages->mapWithKeys(function ($class, $language) {
                    return [
                        $language => [
                            'class'      => $class,
                            'standalone' => true,
                            'embeddable' => true,
                        ]
                    ];
                });
        }
    }
}
