#
# wigwam engine functionality test module.. 

    use strict; 
    use warnings;
    use Text::Wigwam::Const(qw/:all/);

    our $VERSION = '0.01';

sub _proto_eflag_test { [ STRING ], [qw/Break/] }
sub _eflag_test {
    my ($Api) = @_;
    $Api->set_global( Break => 1 );
    return $Api->get_arg;
}

sub _proto_dolist { [ TERM|ANY ], [qw/Break Continue/] }
sub _dolist { $_[0]->get_arg }

sub _proto_kill_block_test { [ ] }
sub _kill_block_test { $_[0]->get_value(q/kill_block_test/) }

my $pasv_token_opt = 0;
sub _proto_pasv_token_opt { $pasv_token_opt += 10; return [ ] }
sub _pasv_token_opt { return $pasv_token_opt++; }

sub _proto_term_test { [ TERM|STRING, TERM|STRING, STRING ] }
sub _term_test {
    my ($Api) = @_;
    return join (
        ";" =>
        join( "," => @{$Api->get_arg} ),
        join( "," => @{$Api->get_arg} ),
        $Api->get_arg,
    );
}

sub _proto_setup_ebbp { [ ] }
sub _setup_ebbp {
    my ($Api) = @_;
    $Api->new_global( BLOCK => q/expr_block_preservation/, 1 );
    return;
}

sub _proto_unbless { [ BLESSED ] }
sub _unbless {
    my ($Api) = @_;
    my $obj = $Api->get_arg;
    my $class = "Unblessed::" . ref($obj);
    bless $obj, $class;
    return $obj;
}

sub _proto_expr_block_preservation { [ EXPR|ANY|BLOCK, EXPR|ANY ] }
sub _expr_block_preservation {
    my ($Api) = @_;
    $Api->set_global( expr_block_preservation => 0 );
    my $on_expr = $Api->get_arg;
    my $off_expr = $Api->get_arg;
    my $block_on = $on_expr->execute;
    my $block_off = $off_expr->execute;
    return( $block_on && !$block_off );
}

sub _proto_get_expr_block_preservation_bit { [ ] }
sub _get_expr_block_preservation_bit {
    my ($Api) = @_;
    return $Api->get_global(q/expr_block_preservation/);
}

sub _proto_get_drip_cache { [ ] }
sub _get_drip_cache {
    my ($Api) = @_;
    return [ @{$Api->{core}} ];
}

sub _proto_get_tokens { [ ] }
sub _get_tokens {
    my ($Api) = @_;
    return [ @{$Api->{core}} ];
}

sub _proto_beaut { [] }
sub _beaut {
    my ($Api) = @_;
    my $root = $Api->root_expr;
    return $root->_rexpr($Api)->_beaut_struct;
}

sub _proto_config { [ ] }
sub _config { $_[0]->config }

sub _proto_get_dtree_cache{ [ ] }
sub _get_dtree_cache{ return $Text::Wigwam::Dtree::drip_cache; }

sub _proto_tokenized_template { [] }
sub _tokenized_template { $_[0]->{core}; }

sub _proto_api { [] }
sub _api { $_[0] }

sub _proto_wwobj { [ STRING ] }
sub _wwobj {
    my ($Api) = @_;
    my $wwobj = Text::Wigwam->new( file => $Api->get_arg )
      or return $Text::Wigwan::ERROR;
    $wwobj->set_path( template => '/var/www/templates/' );
    $wwobj->_init;
    return $wwobj->{engine};
}

sub _proto_setup_call { [] }
sub _setup_call {
    my ($Api) = @_;
    $Api->set_value(
        'call.varspace',
#        Text::Wigwam::Varspace->new(
            {
                foo => 'manchu',
                code => sub {
                    my $arg = shift || '';
                    return "blah!$arg";
                },
            },
#        )
    );
    return;
}

sub _proto_testobj { [ ] }
sub _testobj { return Text::Wigwam::Test->new; }

sub _proto_struct { [] }
sub _struct {
    return {
        HASH => { KEY => 'hash' },
        ARRAY => [ 'array' ],
        CODE => sub { uc $_[0] },
        OBJ => bless {}, 'FOO',
    };
}

package FOO;
    # Used by the #struct directive, above.
    sub test { "method" }

return sub {
    my ($Api) = @_;
    $Api->new_global( BLOCK => q/expr_block_preservation/, 1 );
}
