<?php

namespace HelpGent\App\Providers;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Models\Form;
use HelpGent\WpMVC\Contracts\Provider;
use HelpGent\App\Providers\Admin\MediaProtectionNoticeProvider;
use WP_REST_Request;

class LocalizationServiceProvider implements Provider {
    public function boot() {
        add_action( 'admin_head', [ $this, 'action_admin_head' ] );
        add_filter( 'helpgent_get_forms', [$this, 'get_forms'] );
        
        // Add filter to allow rich text editor
        add_filter( 'user_can_richedit', '__return_true' );
    }

    public function get_forms( array $form_data ) {
        if ( ! current_user_can( 'manage_options' ) ) {
            return $form_data;
        }

        $form_data['admin_total_forms'] = apply_filters( 'helpgent_admin_form_tab_total_query', Form::query( 'form' ) )->distinct()->count( 'form.id' );

        return $form_data;
    }

    public function action_admin_head() : void {
        $data                                      = helpgent_common_localizations();
        $data['site_verification_token']           = helpgent_get_site_verification_token();
        $data['license']                           = helpgent_get_licensing_config();
        $data['nginx_settings_notice_button_data'] = MediaProtectionNoticeProvider::nginx_settings_notice_button_data();
        ?>
        <script data-cfasync="false" type="text/javascript"> // Added data-cfasync to ignore Cloudflare rocket script loader cache
            var helpgent_localization = <?php helpgent_render( json_encode( $data ) );?>
        </script>
        <?php
    }
}