package # hide from PAUSE
    DBIx::Class::ResultSourceProxy;

use strict;
use warnings;

use base qw/DBIx::Class/;
use Scalar::Util qw/blessed/;
use Sub::Name qw/subname/;
use namespace::clean;

__PACKAGE__->mk_group_accessors('inherited_ro_instance' => 'source_name');

sub get_inherited_ro_instance {  shift->get_inherited(@_) }

sub set_inherited_ro_instance {
  my $self = shift;

  $self->throw_exception ("Cannot set @{[shift]} on an instance")
    if blessed $self;

  $self->set_inherited(@_);
}


sub add_columns {
  my ($class, @cols) = @_;
  my $source = $class->result_source_instance;
  $source->add_columns(@cols);
  foreach my $c (grep { !ref } @cols) {
    # If this is an augment definition get the real colname.
    $c =~ s/^\+//;

    $class->register_column($c => $source->column_info($c));
  }
}

sub add_column { shift->add_columns(@_) }


sub add_relationship {
  my ($class, $rel, @rest) = @_;
  my $source = $class->result_source_instance;
  $source->add_relationship($rel => @rest);
  $class->register_relationship($rel => $source->relationship_info($rel));
}


# legacy resultset_class accessor, seems to be used by cdbi only
sub iterator_class { shift->result_source_instance->resultset_class(@_) }

for my $method_to_proxy (qw/
  source_info
  result_class
  resultset_class
  resultset_attributes

  columns
  has_column

  remove_column
  remove_columns

  column_info
  columns_info
  column_info_from_storage

  set_primary_key
  primary_columns
  _pri_cols
  sequence

  add_unique_constraint
  add_unique_constraints

  unique_constraints
  unique_constraint_names
  unique_constraint_columns

  relationships
  relationship_info
  has_relationship
/) {
  no strict qw/refs/;
  *{__PACKAGE__."::$method_to_proxy"} = subname $method_to_proxy => sub {
    shift->result_source_instance->$method_to_proxy (@_);
  };
}

1;
