package DBIx::Data::Collection::Set::Wrapper::Inflate;

use strictures 1;

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

sub _inflator { shift->{inflator} }
sub _inner { shift->{inner} }

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

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

sub clear { shift->_inner->clear }

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

sub replace {
  my ($self, $spec, $body) = @_;
  my $inflator = $self->_inflator;
  $self->_inner->replace(
    $inflator->deflate_spec($spec),
    $inflator->deflate_body($body),
  );
}

sub add {
  my ($self, $body) = @_;
  my $inflator = $self->_inflator;
  $inflator->inflate(
    $self->_inner->add(
      $inflator->deflate_body($body)
    )
  );
}

sub remove {
  my ($self, $spec) = @_;
  $self->_inner->remove($self->_inflator->deflate_spec($spec));
}

sub map {
  require DBIx::Data::Collection::Set::Mapped;
  DBIx::Data::Collection::Set::Mapped->new({
    inner => $_[0], mapper => $_[1]
  });
}

1;
