package DX::Result;

use Safe::Isa;
use Moo;

has _state => (is => 'ro', required => 1, init_arg => 'state');

sub var_names {
  sort keys %{$_[0]->_state->scope};
}

sub actions {
  my ($self) = @_;
  my $state = $self->_state;
  my $by_id = $self->_state->by_id;
  return map $_->required_action,
           grep $_->has_required_action,
             grep $_->$_does('DX::Role::Fact'),
               map $state->resolve_value($_),
                 values %$by_id;
}

sub independent_actions {
  my ($self) = @_;
  return grep !@{$_->dependencies}, $self->actions;
}

sub expand_action_dependencies {
  my ($self, $action) = @_;
  my $by_id = $self->_state->by_id;
  return map $by_id->{$_}->action, @{$action->dependencies};
}

sub value_for {
  my ($self, $name) = @_;
  my $state = $self->_state;
  $state->resolve_value($state->scope_var($name));
}

sub all_values {
  my ($self) = @_;
  return +{ map +($_ => $self->value_for($_)), $self->var_names };
}

1;
