<?php

/**
 * @package     Dotclear
 *
 * @copyright   Olivier Meunier & Association Dotclear
 * @copyright   AGPL-3.0
 */
declare(strict_types=1);

namespace Dotclear\Theme\customCSS;

use Dotclear\App;
use Dotclear\Helper\Process\TraitProcess;

/**
 * @brief   The module backend process.
 * @ingroup customCSS
 */
class Backend
{
    use TraitProcess;

    public static function init(): bool
    {
        return self::status(My::checkContext(My::BACKEND));
    }

    public static function process(): bool
    {
        if (!self::status()) {
            return false;
        }

        App::behavior()->addBehavior('adminPageHTMLHead', function (): string {
            if (App::blog()->settings()->get('system')->getStr('theme', false) !== My::id()) {
                return '';
            }

            if (App::task()->checkContext('MODULE')
                && App::task()->checkContext('THEME_CONFIG')
                && App::auth()->prefs()->get('interface')->getBool('colorsyntax')
            ) {
                $theme = App::auth()->prefs()->get('interface')->getStr('colorsyntax_theme', false);
                echo App::backend()->page()->jsLoadCodeMirror($theme);
            }

            return '';
        });

        return true;
    }
}
