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

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

     # Array-based drip class (DiRective Info Packet)
    use Text::Wigwam::Const( qw/ANY/ );
    use Text::Wigwam::Engine::Fusion::Const( qw/FAKED/ );

    use constant TYPE => 0;
    use constant ARGS => 1;
    use constant TOKN => 2;
    use constant CALL => 3;
    use constant VARS => 4;
    use constant MATE => 5;
    use constant ERRO => 6;

sub expand { @{$_[0]} }
sub type    { $_[0]->[0] || 0 }
sub args    { $_[0]->[1] }
sub token   { $_[0]->[2] }
sub handler { $_[0]->[3] }
sub vars    { $_[0]->[4] }
sub mate    { $_[0]->[5] }
sub error   { $_[0]->[6] }

sub pseudo {
    return bless [
        FAKED,
        [ $_[1] || ANY ],
    ];
}

sub new {
    my ($class, $tokn, $Api, $fake) = @_;
    my $name = $Api->interpolate( my $dname = $tokn->data );

    my $drip = _make_drip(
        $class,
        $tokn,
        $name,
        $Api,
        $fake,
    );

    return (                            # Reasons not to cache the drip:
        $fake                                    # recovery mode
     || $name ne $dname                          # bracketted directive
     || ($Api->get_options(q/drip_cache/) & 2 )  # explicitly disabled
    ) ? $drip : $tokn->dcache($drip);
}

sub _make_drip {
     no strict "refs";
    my ($class, $tokn, $name, $Api, $fake) = @_;
    my ($call, $subr, $arginfo, $localvars, $err)
     = $Api->dtree->query(
        $name,
        q/Engine::Fusion::Directives/,
        0,
    );
    return bless [
        $tokn->type,
        $arginfo,
        $call,
        $subr,
        $localvars,
        $tokn->mate,
    ], $class unless $err;

    $Api->exception( $err ) unless $fake;
    return $class->null( $tokn, $err );
}

sub null {
    my ($drip, $tokn, $err) = @_;
    return bless [
        FAKED | $tokn->type,
        [],
        q/__null/,
        sub {},
        [],
        $err,
    ]
}

1;
