use v6.d;
use RakuDoc::Templates;
use Test;
use Test::Output;

#test-output-verbosity(:on);

plan 13;

my %globals is Template-directory = %(
    aaa => -> %prm, $tmpl {
        'orig: ' ~ %prm<contents>;
    },
    bbb => -> %prm, $tmpl { '<bb-aaa-tab>' ~ $tmpl('aaa') ~ '</end-tab>' },
    ccc => -> %prm, $tmpl { '<cc-aaa-tab>' ~ $tmpl('aaa', %(:contents('never'),)) ~ '</end-tab>' },
    ggg => -> %prm, $tmpl { '<start>' ~ %prm<contents> ~ '<end>' },
    hhh => -> %prm, $tmpl { '<hst>' ~ %prm<contents> ~ '<hend>' },
);

%globals.verbose = 'aaa';

my $rv;
my $out = output-from { $rv = %globals<aaa>(%(:contents<hi there>, :extra<not used>)) };
like $out,
        / 'orig: hi there' /,
        'verbose outputs template results';
like $out, /
    'contents' \s+ 'hi there' .+
    'extra' \s+ 'not used'
    /, 'also outputs the params given to template';
is $rv, 'orig: hi there', 'only returns the template value';
output-like { $rv = %globals<bbb>(%(:contents<bold statement>,)) },
        / 'orig: bold statement' /,
        'verbose only returns the result of the embedded template';
is $rv, '<bb-aaa-tab>orig: bold statement</end-tab>',
        'the returned result contain the extra';
output-like { $rv = %globals<ccc>(%()), '<cc-aaa-tab>orig: never</end-tab>' },
        /  'orig: never' /,
        'verbose is called with new parameters';
is $rv, '<cc-aaa-tab>orig: never</end-tab>', 'returns with different 0 parameters';
output-is { $rv = %globals<ggg>(%(:contents<hi there>)) },
        '',
        'verbose only outputs for the aaa template';
is $rv, '<start>hi there<end>', 'but returns the called ggg template';

%globals.verbose = 'ggg';
$rv = output-from { %globals<ggg>(%(:contents<hi there>, :extra-ggg<not used>)) };
like $rv, / 'extra-ggg' /,
        'verbose outputs for the ggg template';
$rv = output-from { %globals<hhh>(%(:contents<hi there>, :extra-hhh<not used>)) };
unlike $rv, / 'extra-hhh' /,
        'verbose does not output for the hhh template';

%globals.verbose = 'ggg hhh';
$rv = output-from { %globals<ggg>(%(:contents<hi there>, :extra-ggg<not used>)) };
like $rv, / 'extra-ggg' /,
        'verbose outputs for the ggg template';
$rv = output-from { %globals<hhh>(%(:contents<hi there>, :extra-hhh<not used>)) };
like $rv, / 'extra-hhh' /,
        'verbose now does output for the hhh template';

done-testing;
