package Tak::Router;

use Moo;

has services => (is => 'ro', default => sub { {} });

sub start_request {
  my ($self, $req, $target, @payload) = @_;
  $req->mistake("Reached router with no target")
    unless $target;
  $req->failure("Reached router with invalid target ${target}")
    unless my $next = $self->services->{$target};
  $next->start_request($req, @payload);
}

sub receive {
  my ($self, $target, @payload) = @_;
  return unless $target;
  return unless my $next = $self->services->{$target};
  $next->receive(@payload);
}

sub register {
  my ($self, $name, $service) = @_;
  $self->services->{$name} = $service;
}

sub deregister {
  my ($self, $name) = @_;
  delete $self->services->{$name}
}

1;
