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

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

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

    sub new {
        my ($class) = @_;
        bless {
            indent => "   ",
            comments => {},
            identify => $_[1] || "",
            message => $_[2] || "",
            comments => { %{$_[3] || {} } },
        }, ref $class || $class;
    }

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

    sub branch {
        my ($self, $indent, $content) = @_;
        return "\n$content" . (
            $indent ? "\n" . ($self->{indent} x $indent)
                    : "\n"
        );
    }
    sub leaf {
        my ($self, $indent, $content) = @_;
        return  "\n".($self->{indent} x $indent)
              . $content
              . "\n"
              . ($self->{indent} x $indent)
        ;
    }

    sub paint {
        my( $self, $type, $data, $addr ) = @_;
        if( length($self->{identify}) && $addr == $self->{identify} ){
            $self->{identify} = "";
            $self->{comments}{$addr} = "";
            return $self->_trim($data).' /* '.($self->{message} || '<-- HERE').' */';
        }
        elsif( my $comm = $self->{comments}{$addr} ){
            delete $self->{comments}{$addr};
            return $self->_trim($data).' /* '.$comm.' */';
        }
        return $self->_trim($data);
    }

    sub _trim {
        my ($self, $data) = @_;
        return $data if $data eq ' ';
        $data =~ s/^\s*(.+)\s*$/$1/s;
        return $data;
        $data =~ s/^\s*//;
        return $data if $data eq ' ';
        $data =~ s/\s*$//;
        return $data;
    }

    sub default_template {
        return Text::Wigwam->new(
            find     => '/Core/Beautify/Text',
            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 $Text::Wigwam::ERROR;
    }

# sub { 'Text::Wigwam::Beautify::Text' };

1;