package DX::Var;

use Moo;

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

has bound_stream => (is => 'ro');

has bound_value => (is => 'lazy', predicate => 1, clearer => 1, builder => sub {
  if (defined(my $next = $_[0]->bound_stream->next)) {
    return $next;
  }
  DX::State->return_from_op('backtrack');
  return;
});

has action => (is => 'ro');

sub is_bound {
  my ($self) = @_;
  $self->has_bound_value || $self->bound_stream;
}

sub with_stream {
  my ($self, $stream) = @_;
  $self->new(%$self, bound_stream => $stream);
}

sub with_value {
  my ($self, $stream) = @_;
  $self->new(%$self, bound_value => $stream);
}

sub with_action {
  my ($self, $action) = @_;
  $self->new(%$self, action => $action);
}

sub copy {
  my ($self) = @_;
  ref($self)->new(%$self);
}

1;
