package Common::Date::Text;

use base 'Common::Date';

sub priority { 850 }

sub type { 'text' }

sub _monthIndex {
    {
        # Abbreviated
        jan => 1,
        feb => 2,
        mar => 3,
        apr => 4,
        may => 5,
        jun => 6,
        jul => 7,
        aug => 8,
        sep => 9,
        oct => 10,
        nov => 11,
        dec => 12,

        # Full names
        january   => 1,
        february  => 2,
        march     => 3,
        april     => 4,
        may       => 5,
        june      => 6,
        july      => 7,
        august    => 8,
        september => 9,
        october   => 10,
        november  => 11,
        december  => 12,

    };
}

sub _patterns {
    my $self = shift;
    my @abMonths;
    my @months;

    foreach my $month ( keys( %{ $self->_monthIndex } ) ) {
        push( @abMonths, $month ) if ( length($month) == 3 );
    }

    foreach my $month ( keys( %{ $self->_monthIndex } ) ) {
        push( @months, $month ) if ( length($month) > 3 || $month eq 'may' );
    }

    return [
        new Common::Date::Pattern(
            pattern => '(\d{1,2})[\- ](' . join( '|', @abMonths ) . ')[\- ](\d{2,4})',
            month   => 2,
            day     => 1,
            year    => 3
        ),
        new Common::Date::Pattern( pattern => '(\d{1,2})[\- ](' . join( '|', @months ) . ')[\- ](\d{4})', month => 2, day => 1, year => 3 ),
        new Common::Date::Pattern( pattern => '(' . join( '|', @months ) . ') (\d{1,2}),\s?(\d{4})',     month => 1, day  => 2, year => 3 ),
        new Common::Date::Pattern( pattern => '(' . join( '|', @months ) . ')\s(\d{4})',                 month => 1, year => 2 ),
        new Common::Date::Pattern( pattern => '(' . join( '|', @abMonths ) . ')[\-\s](\d{2,4})',         month => 1, year => 2 ),
        new Common::Date::Pattern( pattern => '(' . join( '|', @abMonths ) . ')\s(\d{1,2}),\s(\d{2,4})', month => 1, day  => 2, year => 3 ),
    ];
}

sub month {
    my $self = shift;

    my $month = lc( $self->{_month} );
    my $index = $self->_monthIndex();

    return $index->{$month};
}

sub year {
    my $self = shift;

    if ( $self->{_year} && $self->{_year} =~ /^\d{2}$/ ) {
        return "20$self->{_year}";
    } else {
        return $self->{_year};
    }
}

1;
