package Text::Wigwam::Fetch;

# Loads templates using a method specified by a key which is passed in via
# the interface.
# Text::Wigwam->new( key => param, ... )

    use strict;
    use warnings;
    our $VERSION = '0.01';
    no strict 'refs';
# Return values:  ( error, identifier, template )

sub file {
    my ($self, $Api, $file) = @_;
    $file = $self->_clean( $file );
    return "Null filename encountered!"
      unless length $file;

    $Api->{document} = $file;
    open ( FILE, '<', $file )
      or return "Error opening template file '$file' - $!";
    local $/;
    my $template = <FILE>;
    close( FILE );
    $Api->{mod_time} = (stat($file))[9];
    return( undef, $file, $template );
}

sub text { return ( undef, undef, $_[2] ) }

sub find {
    my ($self, $Api, $file) = @_;
    my $finder = $Api->paths->finder(q/template/);
    $file = $self->_clean( $file );
    my $fspec = $finder->file( $file, qw/ .wwt .wgt / )
        or return   "Unable to locate $file in template path "
                  . join( ", " => @{$finder->get} )
    ;
    return $self->file($Api, $fspec);
}

sub _clean {
    my ($self, $file) = @_;
    $file =~ s/^\s+//;
    $file =~ s/\s+$//;
    return $file;
}

1;
