package BookPub::Import::Importer::AudiobookStore::v2;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin   => 'A2',
        rAuthor     => 'B',
        rTitle      => 'A',
        rIsbn13     => 'C',
        countryCode => 'D',
        rUnits      => 'F',
        rListPrice  => 'E',
        rRevenue    => 'G',
    };
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName       { 'AudioBookStore.com' }
sub priceType          { 'rrp' }
sub currencyCode       { 'USD' }
sub rListPriceCurrency { 'USD' }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rProductType       { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub productType        { shift->rProductType }

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 _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^(\w{3})(?:\w{1,6})?\s(\d{4})/ ) {
        my $day = 1;
        my $year  = $2;
        my $month = $months{ lc $1 };
        $date = sprintf( "%04d-%02d-%02d", $year, $month, $day );
    }

    return $date;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;

    return $self->rIsbn13() && $self->rTitle();
}

1;
