package BookPub::Import::Importer::Amazon::Version28;

use strict;

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

sub _headerIdentifier { ( rIsbn10 => 'ISBN' ) }

sub _fieldMap {
    {
        countryCode        => 'AD',
        rIsbn10            => 'C',
        rIsbn13            => 'D',
        rTitle             => 'I',
        rAuthor            => 'H',
        rUnits             => 'J',
        rListPrice         => 'K',
        rListPriceCurrency => 'AE',
        rDiscount          => 'M',
        rRevenue           => 'AJ',
    };
}

sub _useIsSaleRec { 1 }

sub rServiceName { 'Amazon' }
sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand }
sub productType  { BookPub::DB::Item::Sale::kProductTypePhysicalBook }
sub currencyCode { 'USD' }

sub _dateNormalization { 'month' }
sub _dateFormat { [ 'numerical', 'numerical_year_first' ] }

# get date from filename, MM-YYYY or YYYY-MM
sub _getDateBegin {
    my $self     = shift;
    my $filename = $self->filename();

    if ( $filename && ( $filename =~ /^(\d{4})-(\d{2})/ ) ) {
        my ( $year, $month ) = ( $1, $2 );
        return sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    } elsif ( $filename && ( $filename =~ /^(\d{2})-(\d{4})/ ) ) {
        my ( $year, $month ) = ( $2, $1 );
        return sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    } else {
        $self->fail( "Couldn't determine date from filename: " . $filename );
    }
}

sub countryCode {
    my $self = shift;
    my $code = $self->_getByFieldName('countryCode') || return;

    if ( $code eq 'JP-GBP' ) {
        my $obj = Common::Country->Get('JP');
        return $obj->alpha2;
    }
    return $code;
}

sub rDiscount {
    my $self = shift;
    my $discount = $self->_getByFieldName('rDiscount') || return;

    return 1 - $discount;
}

1;
