
package Text::Wigwam::Engine::Fusion::Token::Terminator::Root;

    use strict;
    use warnings;
    our $VERSION = '0.01';

    use base q/Text::Wigwam::Cast/;

    use Text::Wigwam::Const( qw/TERM/ );
    use Text::Wigwam::Engine::Fusion::Const(
      qw/
        TEXT_BLOCK
        ROOT_BLOCK
        CODE_BLOCK
      /
    );

    sub new { return bless { DRAW => $_[1] } }
    sub exec {
        my ( $tokn, $want, $Arg, $Api, $core ) = @_;
        $Arg->end_list(1); # Always set the list terminator flag
        $Api->exception(
          "Unexpectedly encountered the end of the template"
        ) if !($Arg->type & ROOT_BLOCK) && $Api->get_options( qw/strict_tags/ );
        return undef;
    }
    sub skip {
        my ( $tokn, $list, $Arg, $Api, $core ) = @_;
         # Strict template code enforcement feature
        $Api->exception(
          "Unexpectedly encountered the end of the template"
        ) if !($Arg->type & ROOT_BLOCK) && $Api->get_options( qw/strict_tags/ );
        return TERM; # Always signal the end of list
    }
    sub paint {
        my ( $tokn, $Arg, $Api, $core ) = @_;
        my $color;
        if ( $Arg->type & ROOT_BLOCK ){
          $color = Text::Wigwam::Engine::Fusion::Paint::Group->new(
            [
              Text::Wigwam::Engine::Fusion::Paint::Token->new(
                $tokn->{DRAW}[0],
                $core->curr_addr,
                'tagentity'
              )
            ]
          );
        }
        else {
           # Terminate the non-ROOT_BLOCK list.
          $color = Text::Wigwam::Engine::Fusion::Paint::Group->new(
            [
            Text::Wigwam::Engine::Fusion::Paint::Token->new(
              (
                ($Arg->type & TEXT_BLOCK) ?
                $Api->get_options(qw/text_term/) || "<!!"
                :
                 # If it's not a TEXT_BLOCK or a CODE_BLOCK, it's unknown
                ($Arg->type & CODE_BLOCK) ?
                   $Api->get_options(qw/code_term/) || "!!]"
                :
                   '#/*'
              ),
              $core->curr_addr,
              'implied'
            )
            ]
          );
        }
        return( $color, 1 );
    }

1;
