package BookPub::Import::Importer::Kobo::Version40;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer::Kobo::Version38';

sub _fieldMap {
    {
        dateBegin                => 'A',
        rProductType             => 'G',
        rAuthor                  => 'O',
        rTitle                   => 'P',
        rIsbn13                  => 'N',
        rImprint                 => 'M',
        rState                   => 'C',
        rPostalCode              => 'F',
        countryCode              => 'B',
        rUnits                   => 'H',
        rAdjustments             => 'AA',
        rListPrice               => 'R',
        rListPriceCurrency       => 'U',
        rPurchasePrice           => 'X',
        rPurchasePriceCurrency   => 'Z',
        rCOGS                    => 'S',
        rRevenue                 => 'AB',
        currencyCode             => 'AC',
        rTaxAmount               => 'AD',
        rPromoCode               => 'J',
        rChannel                 => 'K',
        rComments                => 'I',
        rNativeListPrice         => 'R',
        rNativeListPriceCurrency => 'U',
    };
}

sub _unverifiedFieldMap {
    {
        nativeLPCurrency => 'U',
        currencyConv     => 'V',
    };
}

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    # M/D/YYYY or M/DD/YYYY
    if ( $date =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }
    # YYYY-MM-DD
    elsif ( $date =~ /^(\d{4})[-\/](\d{1,2})[-\/](\d{1,2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

    $self->fail("Unable to determine date begin from: '$date'.");
}

sub isFree {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    my $rUnits   = $self->_getByFieldName('rUnits')   || 0;

    if ( !$rRevenue && $rUnits ) {
        return 'Y';
    }

    return 'N';
}


1;
