package Text::Wigwam::Engine::Fusion;

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

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

    use Text::Wigwam::Engine::Fusion::Argument;
    use Text::Wigwam::Engine::Fusion::Beautify;
    use Text::Wigwam::Engine::Fusion::Bugs;
    use Text::Wigwam::Engine::Fusion::Directives;
    use Text::Wigwam::Engine::Fusion::Drip;
    use Text::Wigwam::Engine::Fusion::Expression;
    use Text::Wigwam::Engine::Fusion::Template;
    use Text::Wigwam::Engine::Fusion::Tokenizer;

sub init {
       no warnings 'uninitialized';
    my ($Api, $text) = @_;

    my $Bugs = Text::Wigwam::Engine::Fusion::Bugs->new(
        $Api->get_options( qw/strict_tags/ ),
    );
    $Api->{ARG} ||= []; # Initialize the Argument stack

    $Api->{core} = Text::Wigwam::Engine::Fusion::Tokenizer->new(
#    $Api->{core} = Text::Wigwam::Engine::Fusion::Core->_new(
        $Api,
        $Bugs,
        $text,
    );
     # Report any bugs that may have been detected during tokenization.
    if( $Bugs->is_fatal ){ $Api->exception( @$_ ) for ($Bugs->list) }
      # ...or simply record them if they aren't serious.
    else { $Api->debug( @$_ ) for ($Bugs->list) }
    return $Api->root_expr;
}

 # Fusion specific public API methods begin here

sub curr_addr { return $_[0]->{core}->curr_addr() }
sub root_expr {
    my ($Api) = @_;
    return $Api->{root_expr} ||=
        Text::Wigwam::Engine::Fusion::Template->new($Api)
    ;
}

sub get_arg { return $_[0]->{ARG}[0]->exec_arg($_[0]) }
#sub next_arg { return $_[0]->{ARG}[0]->exec_arg($_[0]) }

sub kill_arg {
    my ($Api, $count) = @_;
    if( ($count) = $count =~ /^(\d+)/ ){
        $Api->{ARG}[0]->skip($Api) while $count--;
        return;
    }
    $Api->exception(
        "kill_arg was called by "
      . $Api->{ARG}[0]->drip->token
      . " with a null or invalid argument!"
    );
    $Api->{ARG}[0]->skip($Api);
    return;
}

 # Private methods begin here

sub _new_arg { unshift( @{$_[0]->{ARG}}, $_[1] ); return $_[1] }
sub _pop_arg { shift @{$_[0]->{ARG}} }

 # Provide the supported config options along with some defaults.

return sub {
     # Return default config options hashref
    return {
        text_open       => '!!>',
        text_term       => '<!!',
        code_open       => '[!!',
        code_term       => '!!]',
        text_open_trim  => '~!>',
        text_term_trim  => '<!~',
        code_open_trim  => '[!~',
        code_term_trim  => '~!]',
        strict_tags     => 1,
        drip_cache      => 0,
        hexadecimal     => 1,
        numbers         => 'FLOAT',
    };
};

__END__

 Fusion-specific config options:
        strict_tags
        numbers
        hexadecimal
        code_open
        code_term
        text_open
        text_term
        code_open_trim
        code_term_trim
        text_open_trim
        text_term_trim
        drip_cache

