# See bottom of file for license and copyright information
package Foswiki;

use strict;
use warnings;

our $ifParser;

sub SWITCH {
    my ( $this, $params, $topicObject ) = @_;

    unless ($ifParser) {
        require Foswiki::If::Parser;
        $ifParser = new Foswiki::If::Parser();
    }

    my $switchval = $params->{_DEFAULT};
    $switchval = '' unless defined $switchval;
    # Recursion guard.
    $this->{evaluating_switch} ||= {};

    # Block after 5 levels.
    if (   $this->{evaluating_switch}->{$switchval}
        && $this->{evaluating_switch}->{$switchval} > 10 )
    {
        delete $this->{evaluating_switch}->{$switchval};
        return '';
    }
    $this->{evaluating_switch}->{$switchval}++;
    my $result = defined $params->{"default"} ? $params->{"default"} : '$switch';
    try {
        for (my $curIdx = 1; $curIdx < 10; ++$curIdx) {
            if (defined $params->{"equals$curIdx"}) {
                if ($switchval eq $params->{"equals$curIdx"}) {
                    $result = $params->{"result$curIdx"}
                        if defined $params->{"result$curIdx"};
                    $curIdx = 10;
                }
            } elsif (defined $params->{"if$curIdx"}) {
                my $query = $params->{"if$curIdx"};
                $query =~ s/\$switch/$switchval/g;
                try {
                    my $expr = $ifParser->parse($query);
                    if ( $expr->evaluate( tom => $topicObject, data => $topicObject ) ) {
                        $result = $params->{"result$curIdx"}
                            if defined $params->{"result$curIdx"};
                        $curIdx = 10;
                    }
                } catch Foswiki::Infix::Error with {
                    my $e = shift;
                    $result =
                      $this->inlineAlert( 'alerts', 'generic', 'SWITCH{...', $params->stringify(),
                        '...}:', $e->{-text} );
                }
            }
        }
    }
    finally {
        delete $this->{evaluating_switch}->{$switchval};
    };
    $result =~ s/\$switch/$switchval/g;
    return expandStandardEscapes($result);
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/

Copyright (C) 2012 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.

Additional copyrights apply to some or all of the code in this
file as follows:

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

As per the GPL, removal of this notice is prohibited.
