package Celaenito::Template::Plugin::URI;
use strict;
use warnings;
use URI;
use URI::QueryParam;
use Template::Plugin;
use Smart::Comments;
our @ISA = qw(Template::Plugin);

sub new {
    my ($class, $context, $base, $args) = @_;
    $args ||= {};

    return sub {
	my $newbase = shift unless ref $_[0] eq 'HASH';
	my $newargs = shift || {};
	my $combo = { %$args, %$newargs };

	my $uri = URI->new($newbase || $base || '');
	while (my ($key, $val) = each %$combo) {
	    defined $val or next;

	    my @val = (ref $val ? @$val : $val);
	    $uri->query_param($key => @val);
	}
	
	return $uri->canonical->as_string;
    };
}

1;
