#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use Data::Dumper;

use lib::abs qw(
    ../lib
);
use Branches;
use Countries;

# global vars

# subs

# main
sub main {

    my $countries = Countries->new();
    my $branches = Branches->new();

    my $b2c = $branches->branch_ids2country_ids();

    my $c2b;

    foreach my $branch_id (sort keys %{$b2c}) {
        foreach my $country_id (sort { $a <=> $b } @{$b2c->{$branch_id}}) {
            push @{$c2b->{$country_id}}, $branch_id,
        }
    }

    my $format = '%-60s %-20s';

    foreach my $country_id (sort { $countries->get_country_name($a, 'ru') cmp $countries->get_country_name($b, 'ru') } keys %{$c2b}) {
        my @branch_ids = @{$c2b->{$country_id}};
        say sprintf $format,
            $countries->get_country_name($country_id, 'ru') . " ($country_id)",
            shift(@branch_ids),
            ;

        foreach my $branch_id (@branch_ids) {
            say sprintf $format,
                '',
                $branch_id,
                ;
        }

    }

}
main();
__END__
