package Text::Wigwam::Api;

    use strict;
    use base 'Text::Wigwam::ApiJr';

    our $VERSION = '0.01';

    use Text::Wigwam::Engine;
    use Text::Wigwam::Nulldata;
    use Scalar::Util( qw/blessed/ );

    sub doc { return $_[0]->{document} || '' }
    sub docname { return $_[0]->doc || 'root template' }
    sub get_options {
        my ( $self, @opts ) = @_;
        my $options = $self->config->cache( q/options/ );
        return %$options
          unless scalar @opts;
        return $options->{$opts[0]}
          unless wantarray;
        return @$options{@opts};
#        my @results;
#        local $_;
#        push( @results, $options->{$_} ) for @opts;
#        return @results;
    }

     # Varspace methods
    sub escape_var { shift->stash->escape(@_) }
    sub get_value { shift->_trap_varspace(get_value => @_) }
    sub interpolate { shift->_trap_varspace(interpolate => @_) }
    sub invalid_var { shift->stash->invalid_var(@_) }
    sub set_value { shift->_trap_varspace(set_value => @_) }
    sub set_alias { shift->_trap_varspace(set_alias => @_) }
    sub undefine { shift->_trap_varspace(undefine => @_) }
    sub is_defined { shift->_trap_varspace(is_defined => @_) }
    sub is_global { shift->_trap_varspace(is_global => @_) }

    sub explicit_get_value { shift->_trap_varspace(explicit_get_value => @_) }
    sub explicit_set_alias { shift->_trap_varspace(explicit_set_alias => @_) }
    sub explicit_is_defined { shift->_trap_varspace(explicit_is_defined => @_) }
    sub explicit_undefine { shift->_trap_varspace(explicit_undefine => @_) }

    sub _trap_varspace {
        my ($Api, $meth, @args) = @_;
        local $@;
        my $val = eval { $Api->stash->$meth(@args, $Api->globals) };
        return $val unless $@;
        $Api->exception($@);
        return;
    }

    sub set_path {
        my ($Api, @args) = @_;
        return $Api->paths->set(@args);
    }

    sub add_path {
        my ($Api, @args) = @_;
        return $Api->paths->add(@args);
    }

    # Spawn a child template
    sub spawn {
        my ($Api, $file, $uvspc) = @_;
        local $Text::Wigwam::ERROR;
        my $paths = $Api->paths;
        my @tpaths = $paths->get( q/template/ ) or do {
            my $err = "No template paths defined - unable to spawn child templates.";
            return($err) if wantarray;
            die $err;
        };

        my $fspec = $paths->find_file( template => $file, qw/.wwt .wgt/ )
          or do {
                my $err = "Unable to locate $file in template path ".join( ", " => @tpaths );
                return($err) if wantarray;
                die $err;
          }
        ;
        my $vspc = $Api->_valid_varspace($uvspc) or do {
            my $err = "The spawn method encountered an invalid varspace "
                    . "parameter - expected a hash reference"
            ;
            return ($err) if wantarray;
            die $err;
        };

        my $wwobj = Text::Wigwam->_new(
            file => $fspec,
            $Api->config->spawn,
            $Api->paths,
            $Api->globals,
            $vspc,
        ) or do {
            return( $Text::Wigwam::ERROR )
              if wantarray;
            die $Text::Wigwam::ERROR;
        };
        return
            wantarray
            ? (undef, $wwobj)
            : $wwobj
        ;
#    ( $ERROR, $obj ) = $self->{engine}->spawn(@args);
#    return ( $ERROR, $obj ) if wantarray;
#    die $ERROR;
    }

    sub _valid_varspace {
        my ($Api, $varspace) = @_;
        return $Api->stash->varspace
          unless defined $varspace;
        return $varspace
          if $Api->is_hash($varspace);
        return;
    }

1;
