package Cpanel::Plugins::Components::whostmgr::all::360_topwidget;

use feature 'state';
use Moo;
use cPstrict;

use Cpanel::Imports;

extends 'Cpanel::Plugins::Components::360::BaseWidget';
with 'Cpanel::Plugins::Components::Role::Template';

has '+js_url' => (
    default => 'plugin_components/360-top-widget/dist/cp-360-top-widget.cmb.min.js',
);

has '+markup' => (
    default => sub {
        require Cpanel::Encoder::Tiny;
        my $title             = Cpanel::Encoder::Tiny::safe_html_encode_str('Server Monitoring');
        my $link_alerts_title = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Alerts") );
        my $text_loading      = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Loading …") );
        my $text_alerts       = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->lextext("[quant,_1,Alert,Alerts,No alerts]") );
        my $text_na           = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Not Available") );
        my $text_unauthorized = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Unauthorized") );
        my $text_not_found    = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Not Found") );
        my $text_server_error = Cpanel::Encoder::Tiny::safe_html_encode_str( locale()->maketext("Server Error") );

        my $is_staging = -e '/var/cpanel/use_monitoring_stage' ? 1                                    : 0;
        my $base_url   = $is_staging                           ? "https://api.stage.monitoring360.io" : "https://api.monitoring360.io";

        my $api_key = Cpanel::Plugins::Components::360::BaseWidget::get_api_key();

        my $base_url_enc = Cpanel::Encoder::Tiny::safe_html_encode_str($base_url);
        my $base_key_enc = Cpanel::Encoder::Tiny::safe_html_encode_str($api_key);

        return <<"END_TEMPLATE";
        <script id="cp-360-top-widget-template" type="text/html">
            <cp-360-top-widget
                title="$title"
                link_alerts_title="$link_alerts_title"
                text_alerts_mt="$text_alerts"
                text_loading="$text_loading"
                text_na="$text_na"
                text_unauthorized="$text_unauthorized"
                text_not_found="$text_not_found"
                text_server_error="$text_server_error"
                api_key="$base_key_enc"
                api_base_url="$base_url_enc">
            </cp-360-top-widget>
        </script>
END_TEMPLATE
    }
);

1;
