package Koha::Plugin::Pt::KEEPS::CatalogueQueryTruncate;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Catalogue Query Truncate',
    author => 'Keep Solutions',
    description => 'Search by authority number, barcode and biblio number using phrase index',
    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 ) = @_;

    return qq%
<script>
    var pathname = window.location.pathname;

    \$(document).ready(function () {
        if (pathname.match("/cgi-bin/koha/authorities/detail.pl")) {
            var biblioLinkSearch = \$("#action a").attr("href");
            biblioLinkSearch = biblioLinkSearch.replace("q=an=", "idx=an,phr&q=");
            \$("#action a").attr("href", biblioLinkSearch);
        }

        if(pathname.match("/cgi-bin/koha/catalogue/search.pl")) {
            \$('select[name="idx"] option[value="bc"]').map(function(i, val){
                \$(val).val("bc,phr");
            });
            setInterval(function() {
                if (\$('select[name="idx"] option[value="control-number"]').length) {
                        \$('select[name="idx"] option[value="control-number"]').map(function(i, val){
                        \$(val).val("control-number,phr");
                    });
                }
            }, 100);
        }
    });
</script>
%;
}

1;