package App::SCS::Plugin::Core::PagePlugin::Template;

use List::Util qw(reduce);
use Moo;

with 'App::SCS::Role::PagePlugin';

has templates => (is => 'ro', required => 1);

has name => (is => 'ro', required => 1);

sub filter_html_zoom {
  my ($self, $zoom) = @_;
  my $template_page = $self->templates->get({ path => $self->name })
    or die "No such template ${\$self->name}";
  my $template_zoom = HTML::Zoom->from_html($template_page->html);
  my @ev;
  my %wrap;
  $template_zoom->select('*[data-wrap]')
                ->collect({ into => \@ev, passthrough => 1 })
                ->then
                ->collect({ filter => sub {
                    my %attrs = %{$ev[0]->{attrs}};
                    my $select = delete $attrs{'data-wrap'};
                    $ev[0] = {
                      %{$ev[0]},
                      raw => undef, raw_attrs => undef,
                      attrs => \%attrs,
                      attr_names => [
                        grep $_ ne 'data-wrap', @{$ev[0]->{attr_names}}
                      ]
                    };
                    $wrap{$select} = \@ev;
                    return $_[0];
                  } })
                ->run;
  reduce {
    my @orig;
    my $sel = $b;
    $a->select($sel)
      ->collect({ into => \@orig, passthrough => 1 })
      ->then
      ->replace(sub {
          my $orig = HTML::Zoom->from_events(\@orig);
          my $wrap = HTML::Zoom->from_events(\@{$wrap{$sel}});
          my @to_replace;
          $wrap->select('*[data-replace]')
               ->collect({ into => \@to_replace, passthrough => 1 })
               ->then
               ->replace(sub {
                   my $sel = $to_replace[0]->{attrs}{'data-replace'};
                   $orig->collect($sel, { into => \my @replace_with })->run;
                   HTML::Zoom::ArrayStream->new({ array => \@replace_with })
                 })
               ->to_stream;
        });
  } $zoom, keys %wrap;
}

1;
