package BookPub::Import::Importer::AlexanderStreet::Version2;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer::AlexanderStreet';

use Date::Calc qw(Days_in_Month);

sub _headerIdentifier { ( rIsbn13 => 'IP Identifier' ) }

sub _fieldMap {
    {
        dateBegin    => 'A3',
        dateEnd      => 'A3',
        rIsbn13      => 'C',
        rTitle       => 'D',
        rAdjustments => 'N',
        rDiscount    => 'K',
        rRevenue     => 'O',
        rDuration    => 'F',
        rModel       => 'E',
    }
}

my %months = (
    jan => 1,
    feb => 2,
    mar => 3,
    apr => 4,
    may => 5,
    jun => 6,
    jul => 7,
    aug => 8,
    sep => 9,
    oct => 10,
    nov => 11,
    dec => 12
);

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';

    #Jul 1, 2018 to Dec 31, 2018
    if ( $date =~ /: (\w{3}) (\d{1,2}), (\d{4}) to/i ) {
        return sprintf "%04d-%02d-%02d", $3, $months{lc($1)}, 1;
    }

    $self->fail("Can't determine dateBegin from: '$date'.");
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    if ( $date =~ /to (\w{3}) (\d{1,2}), (\d{4})$/i ) {
        return sprintf "%04d-%02d-%02d", $3, $months{lc($1)}, Days_in_Month( $3, $months{lc($1)} );
    }

    $self->fail("Can't determine dateEnd from: '$date'.");
}

1;
