
package Text::Wigwam::Engine::Totem::Beautify;
    use strict;
    use warnings;
    no warnings q/uninitialized/;
    our $VERSION = '0.01';

sub new { return bless { BEAU => $_[1], SUBL => [] }; }
sub spew_thing {
    my ($self, $struct) = @_;
    return $self->_beau->branch(
        0,
        ($self->_spew_thing($struct, 1))[1]
    );
    return $self->_beau->list_on(0)
        .  $self->_spew_thing($struct, 1)
        .  $self->_beau->list_off(0)
    ;
#    return $self->_spew_thing($struct, 1);
#    return join( " " => $self->_spew_thing );
}

sub _spew_thing {
    my ($self, $thing, $first, $item) = @_;
    my $beau = $self->_beau;

    if ( $thing->type eq 'sublist' ){
        $self->_temp_subl( $thing->size > 1 ? 1 : 0 );
        my ($slm, $spew) = $self->_spew_sublist($thing, $first);
        $self->_temp_subl;
        my $indent = $self->_indent;
        my @data = $slm
         ? (
             $beau->branch(
                $indent-1,
                 join( "" => map { $beau->leaf($indent, $_ ) } @$spew ),
             )
           )
         :
           ( map { ( " ", $_ ) } @$spew, )
        ;

        return(
            $slm,
            join( "" => @data )
        );
    }

    elsif ( $thing->type eq 'itemlist' ){
        my ($slm, $spew) = $self->_spew_subitem( $thing );
        my @data = (
            $slm || $first
            ?  $item
                ? (
                    map { ( ' ', $_ ) } @$spew,
##                  $self->_joinilist( $spew )
                  )
                : (
                    $beau->leaf(
                        $self->_indent,
                        join( " ", @$spew ),
                    )
                  )
            :  (
                  join( " ", @$spew )
               )
        );

        return(
            $self->_subl || $slm,
            join( "" => @data )
        );
    }

    my @data = $first
     ?
      (
        $beau->leaf(
            $self->_indent,
            $beau->paint(
                $thing->type,
                $thing->data,
                $thing->addr,
            ),
        )
      )
     :
      (
        $beau->paint(
            $thing->type,
            $thing->data,
            $thing->addr,
        )
      )
    ;

    return (
        0,
        join( "" => @data ),
    );
}

sub _spew_sublist {
    my ($self, $sublist) = @_;
    my $slist = 0;
    my @spew = ();
    for my $data ( @{$sublist->data} ){
        my ($slm, $text) = $self->_spew_thing( $data, "", 1 );
        push( @spew, $text );
        $self->_subl( $slm ? 1 : 0 );
        $slist ||= $slm;
    }
    return( $slist, \@spew );
}

sub _spew_subitem {
    my ($self, $item) = @_;
    my @spew = ();
    my $slist = 0;
    for my $data ( @{$item->data} ){
        my ($slm, $text) = $self->_spew_thing( $data, "", 0 );
        push( @spew, $text );
        $slist ||= $slm;
    }
    return( $slist, \@spew );
}

sub _temp_subl {
    my ($self, @args) = @_;
    if( scalar @args ){
        unshift( @{$self->{SUBL}}, @args );
        return;
    }
    shift @{$self->{SUBL}};
}

sub _subl {
    my ($self, $arg) = @_;
    $self->{SUBL}[0] = $arg
      if length $arg;
    return $self->{SUBL}[0] || 0;
}

sub _indent { scalar @{$_[0]->{SUBL}} }

sub _beau { $_[0]->{BEAU} }

1;
