<%ARGS>
$Format => RT->Config->Get('AssetSearchFormat')

%catalogs => ()

$Face => undef
$Size => undef
$Link => undef
$Title => undef

$AddCol => undef
$RemoveCol => undef
$ColUp => undef
$ColDown => undef

$SelectDisplayColumns => undef
$CurrentDisplayColumns => undef
</%ARGS>
<%init>
# This can't be in a <once> block, because otherwise we return the
# same \@fields every request, and keep tacking more CustomFields onto
# it -- and it grows per request.

# All the things we can display in the format string by default
my @fields = qw(
    id Name Description Status
    CreatedBy LastUpdatedBy

    Created     CreatedRelative
    LastUpdated LastUpdatedRelative

    RefersTo    ReferredToBy
    DependsOn   DependedOnBy
    MemberOf    Members
    Parents     Children

    Owner HeldBy Contacts

    NEWLINE
    NBSP
); # loc_qw

my $CustomFields = RT::CustomFields->new( $session{'CurrentUser'});
foreach my $id (keys %catalogs) {
    # Gotta load up the $catalog object, since catalogs get stored by name now.
    my $catalog = RT::Catalog->new($session{'CurrentUser'});
    $catalog->Load($id);
    next unless $catalog->Id;
    $CustomFields->LimitToCatalog($catalog->Id);
    $CustomFields->SetContextObject( $catalog ) if keys %catalogs == 1;
}
$CustomFields->LimitToCatalog(0);

while ( my $CustomField = $CustomFields->Next ) {
    push @fields, "CustomField.{" . $CustomField->Name . "}";
}

$m->callback( Fields => \@fields, ARGSRef => \%ARGS );

my ( @seen);

$Format ||= RT->Config->Get('AssetSearchFormat');
my @format = $m->comp('/Elements/CollectionAsTable/ParseFormat', Format => $Format);
foreach my $field (@format) {
    # "title" is for columns like NEWLINE, which doesn't have "attribute"
    $field->{Column} = $field->{attribute} || $field->{title} || '<blank>';
    push @seen, $field;
}

if ( $RemoveCol ) {
    # we do this regex match to avoid a non-numeric warning
    my ($index) = ($CurrentDisplayColumns // '') =~ /^(\d+)/;
    if ( defined($index) ) {
        delete $seen[$index];
        my @temp = @seen;
        @seen = ();
        foreach my $element (@temp) {
            next unless $element;
            push @seen, $element;
        }
    }
}
elsif ( $AddCol ) {
    if ( defined $SelectDisplayColumns ) {
        my $selected = $SelectDisplayColumns;
        my @columns;
        if (ref($selected) eq 'ARRAY') {
            @columns = @$selected;
        } else {
            push @columns, $selected;
        }
        foreach my $col (@columns) {
            my %column = ();
            $column{Column} = $col;

            if ( $Face eq "Bold" ) {
                $column{Prefix} .= "<b>";
                $column{Suffix} .= "</b>";
            }
            if ( $Face eq "Italic" ) {
                $column{Prefix} .= "<i>";
                $column{Suffix} .= "</i>";
            }
            if ($Size) {
                $column{Prefix} .= "<" . $m->interp->apply_escapes( $Size,  'h' ) . ">";
                $column{Suffix} .= "</" . $m->interp->apply_escapes( $Size, 'h' ) . ">";
            }
            if ( $Link eq "Display" ) {
                $column{Prefix} .= q{<a HREF="__WebPath__/Asset/Display.html?id=__id__">};
                $column{Suffix} .= "</a>";
            }

            if ($Title) {
                $column{Suffix} .= "/TITLE:" . $m->interp->apply_escapes( $Title, 'h' );
            }
            push @seen, \%column;
        }
    }
}
elsif ( $ColUp ) {
    my ($index) = ($CurrentDisplayColumns // '') =~ /^(\d+)/;
    if ( defined $index && ( $index - 1 ) >= 0 ) {
        my $column = $seen[$index];
        $seen[$index]       = $seen[ $index - 1 ];
        $seen[ $index - 1 ] = $column;
        $CurrentDisplayColumns     = $index - 1;
    }
}
elsif ( $ColDown ) {
    my ($index) = ($CurrentDisplayColumns // '') =~ /^(\d+)/;
    if ( defined $index && ( $index + 1 ) < scalar @seen ) {
        my $column = $seen[$index];
        $seen[$index]       = $seen[ $index + 1 ];
        $seen[ $index + 1 ] = $column;
        $CurrentDisplayColumns     = $index + 1;
    }
}


my @format_string;
foreach my $field (@seen) {
    next unless $field;
    my $row = "";
    if ( $field->{'original_string'} ) {
        $row = $field->{'original_string'};
    }
    else {
        $row .= $field->{'Prefix'} if defined $field->{'Prefix'};
        $row .= "__$field->{'Column'}__"
          unless ( $field->{'Column'} eq "<blank>" );
        $row .= $field->{'Suffix'} if defined $field->{'Suffix'};
        $row =~ s!([\\'])!\\$1!g;
        $row = "'$row'";
    }
    push( @format_string, $row );
}

$Format = join(",\n", @format_string);


return($Format, \@fields, \@seen);

</%init>
