package DX::Action::SetBoundValue;

use DX::Update::SetValue;
use DX::Class;

with 'DX::Role::Action';

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

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

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

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

sub dry_run {
  my ($self, $hyp) = @_;
  my ($outer_hyp, @inner_events) = $self->inner_action->dry_run(
    $hyp->but(
      action_applications => [ @{$hyp->action_applications}, $self ],
    ),
  );
  my ($scope, @events) = $outer_hyp->scope->apply_updates(
                           DX::Update::SetValue->new(
                             target_path => $self->target_path,
                             new_value => $self->new_value,
                           )
                         );
  my $new_bound = do {
    my $targ = $scope;
    $targ = $targ->get_member_at($_) for @{$self->rebind_path};
    $targ;
  };
  my @actions = @{$outer_hyp->actions};
  foreach my $idx (0.. $#actions) {
    my $act = $actions[$idx];
    if (
      $act->isa('DX::Action::BindValue')
        and join("\0", @{$act->target_path})
              eq join("\0", @{$self->rebind_path})
    ) {
      my $bind = splice @actions, $idx, 1;
      push @actions, $bind->but(new_value => $new_bound);
      last;
    }
  }
  return (
    $outer_hyp->but(scope => $scope, actions => \@actions),
    @inner_events, @events
  );
}

sub run { die }

1;
