package App::SCS::Plugin::Feeds;

use Module::Runtime qw(use_module);
use Moo;

with 'App::SCS::Role::Plugin';

has mount_at => (is => 'ro', default => sub { 'feed' });

has generator => (
  is => 'lazy',
  handles => { _feed_http_response => 'feed_http_response' },
);

sub _build_generator {
  my ($self) = @_;
  use_module('App::SCS::Plugin::Feeds::Generator')->new(
    pages => $self->pages,
    mounted_at => $self->mount_at,
  );
}

sub page_dispatchers {
  my ($self) = @_;
  my $base = $self->mount_at;
  "/${base}/**/" => sub {
    if (my $conf = $self->config->{$_[1]}) {
      $conf = { base => $_[1], %$conf };
      $self->_feed_http_response(200 => $conf => $_[-1]);
    }
  },
}

sub provides_pages {
  my ($self) = @_;
  my $base = $self->mount_at;
  return map "/${base}/$_/", keys %{$self->config};
}

1;
