package Text::Wigwam::Beautify::Html;

    use strict;
    use warnings;
    no warnings q/uninitialized/;
    our $VERSION = '0.01';

    use base( q/Text::Wigwam::Beautifier/ );

sub new {
    my ($class, $ident, $msg, $comm, $uniq) = @_;
    local $Text::Wigwam::ERROR;
    return bless {
        identify  => $ident,
        message   => $msg,
        comments  => { %{$comm||{}} },
        unique_id => $uniq || 0,
    }, ref($class) || $class;
}

sub add_comments {
    my ($self, $hash) = @_;
    return unless ref($hash) eq 'HASH';
    $self->{comments} = {
        %$hash,
        %{ $self->comments || {} }, # Give priority to existing comments
    };
}

sub comments { $_[0]->{comments} }

sub branch { shift->dispatch( [branch => @_] ) }
sub leaf { shift->dispatch( [leaf => @_] ) }
sub paint {
    my( $self, $type, $data, $addr ) = @_;
    my ($mark, $note);
    if( length($self->{identify}) && $addr == $self->{identify} ){
        $mark = $self->{message} || '<-- HERE';
        delete $self->{identify};
        delete $self->{comments}{$addr};
    }
    else {
        $note = $self->{comments}{$addr};
        delete $self->{comments}{$addr};
    }
    return $self->dispatch(
      [
        paint => (
          $type,
          $data,
          $addr,
          $mark,
          $note,
          $self->{unique_id},
        ),
      ]
    );
}

sub dispatch {
    my ($self, @args) = @_;
    my ($err, $txt) = $self->_dispatcher->execute( @args );
    return $txt unless $err;
    die $err;
}

sub _dispatcher {
    my ($self, @args) = @_;
    return $self->{dispatcher}
       ||= $self->__dispatcher(@args);
}

sub __dispatcher {
    my ($self, $varspace) = @_;
    my $dispatcher = Text::Wigwam->new(
        find        => 'Core/Beautify/Html/default',
        restrict => {
            config_open    => q/<!--/,
            config_term    => q/-->/,
            default_engine => q/Totem, Fusion/,
        },
        defaults => {
            strict_tags    => 1,
            text_open      => q/!!>/,
            text_term      => q/<!!/,
            code_open      => q/[!!/,
            code_term      => q/!!]/,
            text_open_trim => q/~!>/,
            text_term_trim => q/<!~/,
            code_open_trim => q/[!~/,
            code_term_trim => q/~!]/,
        },
        settings => {
            plugins  => 'DirectiveSet',
        },
    ) or die "Unable to load dispatch template - $Text::Wigwam::ERROR";
    my ($err) = $dispatcher->execute(); # Initialize the dispatch template
    die "Dispatch initialization error - $err" if $err;
    return $dispatcher;
}

sub default_template {
    return Text::Wigwam->new(
        find     => '/Core/Beautify/Html',
        defaults => {
            engine  => 'Totem',
        },
    ) or die $Text::Wigwam::ERROR;
}

1;

__END__
        restrict => {
             code_open      => q/[!!/,
             code_term      => q/!!]/,
             text_open      => q/!!>/,
             text_term      => q/<!!/,
             code_open_trim => q/[!~/,
             code_term_trim => q/~!]/,
             text_open_trim => q/~!>/,
             text_term_trim => q/<!~/,
        },
