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

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

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

    use Text::Wigwam::Engine::Totem::Const( qw/TEXT_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( $Arg->type == TEXT_BLOCK ){
       bless $tokn, 'Text::Wigwam::Engine::Totem::Token::Terminator::Fast'
         if $Api->get_options( qw/strict_tags/ );
        $Arg->end_list(1);
        return;
    };

     # 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 text terminator."
    );
    $Arg->end_list(1);
    return;
}

sub skip {
    my ($tokn, $list, $Arg, $Api, $core) = @_;
    $core->next_token;

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

    if( $Api->get_options(q/strict_tags/) ){
        $Api->exception(
          "Encountered an unexpected text terminator"
        );
        return TERM;
    }
    $Api->debug(
        "Encountered an unexpected text terminator"
    );
    return $core->curr_token->skip( $list, $Arg, $Api, $core )
}

sub paint {
    my ($tokn, $Arg, $Api, $core) = @_;
    $core->next_token;
    $tokn->{PAINT} ||= Text::Wigwam::Engine::Totem::Paint::Group->new(
        [
          Text::Wigwam::Engine::Totem::Paint::Token->new(
            $tokn->{DRAW}[0], # <!!
            $core->curr_addr,
            $tokn->type,
          )
        ]
    );
    return(
        Text::Wigwam::Engine::Totem::Paint::Group->new(
            [ @{$tokn->{PAINT}} ],
        ),
        1,
    ) if( $Arg->type == TEXT_BLOCK ); # All's good

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

1;
