package DX::Op::MemberLookup;

use Moo;

with 'DX::Role::Op';

has member_var => (is => 'ro', required => 1);
has coll_name => (is => 'ro', required => 1);
has key_name => (is => 'ro', required => 1);
has key_var => (is => 'ro', required => 1);

sub run {
  my ($self, $state) = @_;
  my ($member, $key) = map $state->scope_var($self->$_), qw(member_var key_var);
  die "key unbound" unless $key->is_bound;
  die "member bound" if $member->is_bound;
  my $set = $state->facts->{$self->coll_name};
  if (my $value = $set->{$key->bound_value}) {
    return $state->bind_var_then($member, $value, $self->next);
  }
  return $state->backtrack;
}

1;
