package DX::Step::ResolveProposition;

use DX::Step::EnterRecheck;
use DX::Step::CompleteResolution;
use DX::Step::Backtrack;

use DX::Utils qw(expand_deps);

use DX::Class;

with 'DX::Role::Step';

has resolution_space => (is => 'ro', isa => ResolutionSpace);

sub but_first {
  my ($self, @actions) = @_;
  $self->but(actions => [ @actions, @{$self->actions} ]);
}

sub but_with_dependencies_on {
  my ($self, @deps) = @_;
  $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
}

sub apply_to {
  my ($self, $old_ss) = @_;
  my $rspace = $self->resolution_space;
  my $prop = $rspace->proposition;
  my $res = $rspace->next_resolution;
  my $vdeps = $res->veracity_depends_on;
  trace resolve => [ statement => [
    [ symbol => 'resolve' ],
    [ block => [
      [ statement => [
        [ symbol => 'proposition' ],
        @{$prop->for_deparse->[1]},
      ] ],
      (@{$res->actions}
        ? [ statement => [
            [ symbol => 'actions' ],
            [ block => $res->actions ],
          ] ]
        : ()),
      [ statement => [
        [ symbol => 'depends_on' ],
        [ block => [
          map [ statement => [
            [ symbol => (split '::', ${$_->[0]})[-1] ],
            [ value_path => [ @{$_}[1..$#$_] ] ]
          ] ], @{$vdeps}
        ] ],
      ] ],
    ] ]
  ] ];
  my $ss = $old_ss->but(
    next_step => DX::Step::CompleteResolution->new(
      original_search_state => $old_ss,
      resolution_space => $rspace,
    )
  );
  my $old_hyp = $old_ss->current_hypothesis;
  (my $hyp, my @recheck) = $old_hyp->with_resolution(
    $prop, $vdeps, $res->actions
  );
  return $ss->but(next_step => DX::Step::Backtrack->new) unless $hyp;
  return $ss->but(current_hypothesis => $hyp) unless @recheck;
  return $ss->but(
    current_hypothesis => $hyp,
    next_step => DX::Step::EnterRecheck->new(
      proposition_list => \@recheck,
      on_completion_step => $ss->next_step,
      resolution_space => $rspace,
    ),
  );
}

1;
