# For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-

package Lingua::RON::Num2Word;
# ABSTRACT: Number 2 word conversion in RON.

use 5.16.0;
use utf8;
use warnings;

# {{{ use block

use Carp;
use Export::Attrs;
use Readonly;

# }}}
# {{{ variable declarations

my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
our $VERSION = '0.2603250';

# }}}

# {{{ num2ron_cardinal                 convert number to text

sub num2ron_cardinal :Export {
    my $positive = shift;

    croak 'You should specify a number from interval [0, 999_999_999]'
        if    !defined $positive
           || $positive !~ m{\A\d+\z}xms
           || $positive < 0
           || $positive > 999_999_999;

    my @units = ('zero', 'unu', 'doi', 'trei', 'patru', 'cinci',
                 'șase', 'șapte', 'opt', 'nouă');

    my @teens = ('zece', 'unsprezece', 'doisprezece', 'treisprezece',
                 'paisprezece', 'cincisprezece', 'șaisprezece',
                 'șaptesprezece', 'optsprezece', 'nouăsprezece');

    my @tens = ('', '', 'douăzeci', 'treizeci', 'patruzeci', 'cincizeci',
                'șaizeci', 'șaptezeci', 'optzeci', 'nouăzeci');

    return $units[$positive]          if ($positive >= 0 && $positive < 10);
    return $teens[$positive - 10]     if ($positive >= 10 && $positive < 20);

    my $out;
    my $remain;

    if ($positive > 19 && $positive < 100) {                   # 20 .. 99
        my $ten_idx = int($positive / 10);
        $remain     = $positive % 10;

        $out = $tens[$ten_idx];
        $out .= ' și ' . $units[$remain] if ($remain);
    }
    elsif ($positive == 100) {                                 # 100
        $out = 'o sută';
    }
    elsif ($positive > 100 && $positive < 200) {               # 101 .. 199
        $remain = $positive % 100;
        $out = 'o sută ' . num2ron_cardinal($remain);
    }
    elsif ($positive >= 200 && $positive < 1000) {             # 200 .. 999
        my $hun_idx = int($positive / 100);
        $remain     = $positive % 100;

        $out = $hun_idx == 2
            ? 'două sute'
            : num2ron_cardinal($hun_idx) . ' sute';
        $out .= ' ' . num2ron_cardinal($remain) if ($remain);
    }
    elsif ($positive >= 1000 && $positive < 2000) {            # 1000 .. 1999
        $remain = $positive % 1000;

        $out = 'o mie';
        $out .= ' ' . num2ron_cardinal($remain) if ($remain);
    }
    elsif ($positive >= 2000 && $positive < 1_000_000) {       # 2000 .. 999_999
        my $tho_idx = int($positive / 1000);
        $remain     = $positive % 1000;

        $out = $tho_idx == 2
            ? 'două mii'
            : num2ron_cardinal($tho_idx) . ' mii';
        $out .= ' ' . num2ron_cardinal($remain) if ($remain);
    }
    elsif ($positive >= 1_000_000 && $positive < 2_000_000) {  # 1_000_000 .. 1_999_999
        $remain = $positive % 1_000_000;

        $out = 'un milion';
        $out .= ' ' . num2ron_cardinal($remain) if ($remain);
    }
    elsif ($positive >= 2_000_000 && $positive < 1_000_000_000) { # 2_000_000 .. 999_999_999
        my $mil_idx = int($positive / 1_000_000);
        $remain     = $positive % 1_000_000;

        $out = $mil_idx == 2
            ? 'două milioane'
            : num2ron_cardinal($mil_idx) . ' milioane';
        $out .= ' ' . num2ron_cardinal($remain) if ($remain);
    }

    return $out;
}

# }}}

1;

__END__

# {{{ POD HEAD

=pod

=encoding utf-8

=head1 NAME

=head2 Lingua::RON::Num2Word

=head1 VERSION

version 0.2603250

Number 2 word conversion in RON.

Lingua::RON::Num2Word is module for converting numbers into their written
representation in Romanian. Converts whole numbers from 0 up to 999 999 999.

Text output is encoded in UTF-8.

=cut

# }}}
# {{{ SYNOPSIS

=pod

=head1 SYNOPSIS

 use Lingua::RON::Num2Word;

 my $text = Lingua::RON::Num2Word::num2ron_cardinal( 123 );

 print $text || "sorry, can't convert this number into Romanian.";

=cut

# }}}
# {{{ Functions Reference

=pod

=head1 Functions Reference

=over 2

=item B<num2ron_cardinal> (positional)

  1   num    number to convert
  =>  str    converted string
      undef  if input number is not known

Convert number to text representation.
Only numbers from interval [0, 999_999_999] will be converted.

=back

=cut

# }}}
# {{{ EXPORTED FUNCTIONS

=pod

=head1 EXPORT_OK

=over 2

=item num2ron_cardinal

=back

=cut

# }}}
# {{{ POD FOOTER

=pod

=head1 AUTHORS

 specification, maintenance:
   Richard C. Jelinek E<lt>rj@petamem.comE<gt>
 maintenance, coding (2025-present):
   PetaMem AI Coding Agents

=head1 COPYRIGHT

Copyright (c) PetaMem, s.r.o. 2002-present

=cut

# }}}
