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

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

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

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

    sub new { return bless { DRAW => $_[1], TYPE => 'tagentity' } }
    sub type { $_[0]->{TYPE} }
    sub exec {
        my( $tokn, $want, $Arg, $Api, $core ) = @_;
        $core->next_token;
        if(
            $want & TERM &&
            ($Arg->type & TEXT_BLOCK) &&
            !($Arg->type & ROOT_BLOCK)
        ){
            bless $tokn, 'Text::Wigwam::Engine::Fusion::Token::Terminator::Fast'
              if $Api->get_options( qw/strict_tags/ );
            $Arg->end_list(1);
            return undef;
        }
         # Strict tag enforcement
        return $core->curr_token->exec( $want, $Arg, $Api, $core )
            unless $Api->get_options( qw/strict_tags/ );
#        return $Arg->exec($Api) unless $Api->get_options( qw/strict_tags/ );
        $Api->exception(
          "Encountered an unexpected block terminator."
        );
        $Arg->end_list(1);
        return;
    }
    sub skip {
        my( $tokn, $list, $Arg, $Api, $core ) = @_;
        $core->next_token;

        if (
            $list &&
            ($Arg->type & TEXT_BLOCK) &&
            !($Arg->type & ROOT_BLOCK)
        ){
            bless $tokn, 'Text::Wigwam::Engine::Fusion::Token::Terminator::Fast'
              if $Api->get_options( qw/strict_tags/ );
            return TERM;
        }

         # Strict template code enforcement feature
        return $core->curr_token->skip( $list, $Arg, $Api, $core )
          unless $Api->get_options( qw/strict_tags/ );
        $Api->exception(
          "Encountered an unexpected block terminator"
        );
        return TERM;
    }
    sub paint {
        my( $tokn, $Arg, $Api, $core ) = @_;
        $core->next_token;
        $tokn->{PAINT} ||= Text::Wigwam::Engine::Fusion::Paint::Group->new(
            [
              Text::Wigwam::Engine::Fusion::Paint::Token->new(
                $tokn->{DRAW}[0], # <!!
                $core->curr_addr,
                'tagentity'
              )
            ]
        );
        return(
            Text::Wigwam::Engine::Fusion::Paint::Group->new(
                [ @{$tokn->{PAINT}} ]
            ),
            1
        )
          if (
            ($Arg->want & TERM) &&
            ($Arg->type & TEXT_BLOCK) &&
            !($Arg->type & ROOT_BLOCK)
          );

         # Don't terminate the list, just return the next token.
        my( $text, $end ) = $core->curr_token->paint( $Arg, $Api, $core );
        return(
            Text::Wigwam::Engine::Fusion::Paint::Group->new(
              [
                Text::Wigwam::Engine::Fusion::Paint::Token->new(
                '(',
                $core->curr_addr,
                'error'
                ),
                @{$tokn->{PAINT}},
                Text::Wigwam::Engine::Fusion::Paint::Token->new(
                '?)',
                $core->curr_addr,
                'error'
                ),
                @$text
              ]
            ),
            $end
        );
    }

1;
