package App::SCS::Role::Plugin;

use Getopt::Long qw(GetOptionsFromArray);
use Moo::Role;

with 'App::SCS::Role::WithConfig';

has app => (
  is => 'ro', weak_ref => 1, required => 1,
  handles => [ 'pages' ],
);

sub page_plugins { () }

sub default_page_plugins { () }

sub page_dispatchers { () }

sub provides_pages { () }

sub register { return }

sub run_cli {
  my ($self, $env) = @_;
  my ($command, @argv) = @{$env->{argv}};
  return unless $command;
  return unless my $code = $self->can(my $meth = "run_command_${command}");
  my $run_env = { %$env };
  if (my $proto = prototype $code) {
    my @spec = split ';', $proto;
    my %opt;
    GetOptionsFromArray(\@argv, \%opt, @spec);
    $run_env->{options} = \%opt;
  }
  $run_env->{argv} = \@argv;
  $self->$meth($run_env);
  return 1;
}

1;
