<commit_before><?php

namespace SilverStripe\SpellCheck\Handling;

use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\SecurityToken;

class SpellCheckMiddleware implements HTTPMiddleware
{
    use Configurable;

    /**
     * HTMLEditorConfig name to use
     *
     * @var string
     * @config
     */
    private static $editor = 'cms';

    public function process(HTTPRequest $request, callable $delegate)
    {
        // Set settings
        $editor = $this->config()->get('editor');

        /** @var TinyMCEConfig $editorConfig */
        $editorConfig = TinyMCEConfig::get($editor);

        $editorConfig->enablePlugins('spellchecker');
        $editorConfig->addButtonsToLine(2, 'spellchecker');

        $token = SecurityToken::inst();

        $editorConfig
            ->setOption('spellchecker_rpc_url', Director::absoluteURL($token->addToUrl('spellcheck/')))
            ->setOption('browser_spellcheck', false)
            ->setOption('spellchecker_languages', implode(',', $this->getLanguages()));

        $defaultLocale = $this->getDefaultLocale();
        if ($defaultLocale) {
            $editorConfig->setOption('spellchecker_language', $defaultLocale);
        }

        return $delegate($request);
    }

    /**
     * Check languages to set
     *
     * @return string[]
     */
    public function getLanguages()
    {
        $languages = [];
        foreach (SpellController::get_locales() as $locale) {
            $languages[] = i18n::getData()->localeName($locale) . '=' . $locale;
        }
        return $languages;
    }

    /**
     * Returns the default locale for TinyMCE. Either via configuration or the first in the list of locales.
     *
     * @return string|false
     */
    public function getDefaultLocale()
    {
        // Check configuration first<commit_msg>FIX Manually replace Maori with Māori (intl bug)<commit_after><?php

namespace SilverStripe\SpellCheck\Handling;

use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\SecurityToken;

class SpellCheckMiddleware implements HTTPMiddleware
{
    use Configurable;

    /**
     * HTMLEditorConfig name to use
     *
     * @var string
     * @config
     */
    private static $editor = 'cms';

    public function process(HTTPRequest $request, callable $delegate)
    {
        // Set settings
        $editor = $this->config()->get('editor');

        /** @var TinyMCEConfig $editorConfig */
        $editorConfig = TinyMCEConfig::get($editor);

        $editorConfig->enablePlugins('spellchecker');
        $editorConfig->addButtonsToLine(2, 'spellchecker');

        $token = SecurityToken::inst();

        $editorConfig
            ->setOption('spellchecker_rpc_url', Director::absoluteURL($token->addToUrl('spellcheck/')))
            ->setOption('browser_spellcheck', false)
            ->setOption('spellchecker_languages', implode(',', $this->getLanguages()));

        $defaultLocale = $this->getDefaultLocale();
        if ($defaultLocale) {
            $editorConfig->setOption('spellchecker_language', $defaultLocale);
        }

        return $delegate($request);
    }

    /**
     * Check languages to set
     *
     * @return string[]
     */
    public function getLanguages()
    {
        $languages = [];
        foreach (SpellController::get_locales() as $locale) {
            $localeName = i18n::getData()->localeName($locale);
            // Fix incorrectly spelled Māori language
            $localeName = str_replace('Maori', 'Māori', $localeName);
            $languages[] = $localeName . '=' . $locale;
        }
        return $languages;
    }

    /**
     * Returns the default locale for TinyMCE. Either via configuration or the first in the list of locales.
     *
     * @return string|false
     */
    public function getDefaultLocale()
    {
        // Check configuration first
{'fraction_non_alphanumeric': 0.12248628884826325, 'fraction_numerical': 0.0031992687385740404, 'mean_word_length': 2.887211367673179, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '233917', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1282, 'n_words': 249}