package BookPub::Import::Importer::Amazon::Version26;

use strict;

use Date::Calc qw( Add_Delta_YM );

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

# Same format as version 15, but with translated headers.

sub _headerIdentifier { ( dateBegin => 'Date de facturation (Invoice Date)' ) }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    if ( $date && $date =~ /(\d{4})-(\d{2})-(\d{2})/ ) {

        my ( $year, $month, $day ) = Add_Delta_YM( $1, $2, $3, 0, -1 );

        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    }
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

sub _isValidSaleRecord {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    # Need to ignore the total line at the bottom of the file.
    return ( $self->rTitle || $self->rAuthor || $self->rIsbn13 ) && ( $self->rUnits || $self->rRevenue ) && $self->dateBegin;
}

###
1;    # Play nicely.
###
