package DX::Op::HasAction;

use Safe::Isa;
use Moo;

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

with 'DX::Role::Op';

sub run {
  my ($self, $state) = @_;
  my @arg_spec = @{$self->arg_spec};
  ($state, my %vars) = $self->_expand_args(
    $state, Thing => $arg_spec[0], Action => $arg_spec[1]
  );
  die "Thing must be bound" unless $vars{Thing}->is_bound;
  my $thing = $state->resolve_value($vars{Thing});
  if ($thing->$_does('DX::Role::Fact') and $thing->has_required_action) {
    return $state->bind_value(
      $vars{Action}->id,
      $state->actions->{$thing->required_action}
    )->add_dependencies($vars{Action}->id => $vars{Thing}->id)
     ->then($self->next);
  }
  return $state->backtrack;
}

1;
