package Koha::Plugin::Pt::KEEPS::UserManual;

use Modern::Perl;

use base qw(Koha::Plugins::Base);

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - User Manual',
    author => 'Keep Solutions',
    description => 'KEEP Solutions custom user manual for Koha',
    date_authored   => '2022-01-26',
    date_updated    => undef,
    minimum_version => '21.05',
    maximum_version => undef,
    version         => $VERSION,
};

sub new {
    my ( $class, $args ) = @_;

    $args->{'metadata'} = $metadata;
    $args->{'metadata'}->{'class'} = $class;

    my $self = $class->SUPER::new($args);

    return $self;
}

# Mandatory even if does nothing
sub install {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub upgrade {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub uninstall {
    my ( $self, $args ) = @_;

    return 1;
}

sub intranet_js {
    my ( $self ) = @_;

    my $path = $self->get_plugin_http_path();

    return qq%
<script>
    var pathname = window.location.pathname;
    
    if (pathname.match("/cgi-bin/koha/mainpage.pl") && \$('.user-manual').length) {
        var user_man_plugin_path = "$path";

        var lang = \$('html').attr('lang');
        if (lang == null) {
            lang = \$('html').attr('xml:lang');
        }

        // Set the global configs to synchronous 
        \$.ajaxSetup({
            async: false
        });

        var TOKENS = {};
        \$.getJSON(user_man_plugin_path + `/i18n/\${lang}.json`, function (data) {
            TOKENS = data;
        }).fail(function() { 
            \$.getJSON(user_man_plugin_path + `/i18n/default.json`, function (data) {
                TOKENS = data;
            }); 
        });

        \$.ajaxSetup({
            async: true
        });

        \$('.user-manual').text(TOKENS.user_manual_label);
        \$('.user-manual').attr("href", user_man_plugin_path + "/user-manual.pl");
    }
</script>
%;
}

1;