package LasTres::PJActions;

use v5.36.0;
use strict;
use warnings;

use Moo;

use Module::Pluggable search_path => ['LasTres::PJAction'],
    instantiate => 'new',
    on_require_error => sub ($plugin, $error) {
        die $error;
    };

has hash => (
    is => 'rw',
    lazy => 1,
    builder => \&_build_hash,
);

sub _build_hash($self) {
    my @actions = $self->plugins();
    my %hash;
    for my $action (@actions) {
        my $set_success = 0;
        if (exists $hash{$action->identifier}) {
            warn "$action could not be added because a action with the "
            . "identifier @{[$action->identifier]} already exists.";
        }
        eval {
            if ($action->does('LasTres::PJAction')) {
                $hash{$action->identifier} = $action;
                $set_success = 1;
            }
        };
        if (!$set_success) {
            warn "$action does not implement the role LasTres::PJAction.";
        }
    }
    return \%hash;
}
1;
