package MooseX::JobControl::Types;
use MooseX::Workers::Job::Command;
use MooseX::Workers::Job::Code;
use Moose::Util::TypeConstraints;

coerce 'MooseX::Workers::Job' => (
    from CodeRef => via {
        require MooseX::Workers::Job;
        MooseX::Workers::Job->new( command => $_ );
    },
    from Str => via {
        require MooseX::Workers::Job;
        MooseX::Workers::Job->new( command => $_ );
    },
);

subtype 'JobQueue' => as 'Object' =>
  where { $_[0]->does('MooseX::JobControl::Interface::JobQueue') };

coerce 'JobQueue' => from 'ArrayRef' => via {
    require MooseX::JobControl::JobQueue::Array;
    MooseX::JobControl::JobQueue::Array->new( job_queue => $_ );
};

my $array_of_jobs = Moose::Meta::TypeConstraint::Parameterized->new(
    name           => 'ArrayOfJobs',
    parent         => find_type_constraint('ArrayRef'),
    type_parameter => find_type_constraint('MooseX::Workers::Job'),
);
Moose::Util::TypeConstraints->get_type_constraint_registry->add_type_constraint(
    $array_of_jobs);

coerce 'ArrayOfJobs' => from 'ArrayRef' => via {
    my $type = find_type_constraint('MooseX::Workers::Job');
    return [ map { $_ = $type->coerce($_); } @$_ ];
};



1;
__END__
