package DBIx::Data::Collection::Set::Mapped;

use strictures 1;

use DBIx::Data::Stream::Mapped;

sub new {
  my $proto = shift;
  bless({ %{$_[0]} }, ref($proto)||$proto);
}

sub _inner { shift->{inner} }
sub _mapper { shift->{mapper} }

sub flatten {
  my ($self) = @_;
  map $self->_do_map($_), $self->_inner->flatten;
}

sub to_stream {
  my ($self) = @_;
  my $mapper = $self->_mapper;
  DBIx::Data::Stream::Mapped->new({
    inner => $self->_inner->to_stream,
    mapper => $self->_mapper
  });
}

sub get {
  my ($self, $spec) = @_;
  if (my $got = $self->_inner->get($spec)) {
    return $self->_do_map($got);
  }
  return undef;
}

sub _do_map {
  my ($self, $to_map) = @_;
  local $_ = $to_map;
  $self->_mapper->($to_map);
}

1;
