package DX::Op::OneEach;

use DX::Op::FromCode;
use Moo;

with 'DX::Role::Op';

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

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

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

sub run {
  my ($self, $state) = @_;
  my ($this, @rest) = @{$self->each_of};
  my $next_op = (@rest ? $self->but(each_of => \@rest) : $self->next);
  my $ret_op = DX::Op::SetScope->new(
    scope => $state->scope, next => $next_op
  );
  $state->assign_vars($self->var_name => { bound_value => $this })
        ->push_return_then($ret_op, $self->each_body);
}

1;
