package BookPub::Import::Importer::RedShelf::Version6;

use strict;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        6 => {
            dateBegin       => 'B',
            rPurchaseType   => 'U',
            purchaseType    => 'U',
            rProductType    => 'T',
            rAuthor         => 'H',
            rTitle          => 'G',
            rIsbn13         => 'I',
            rImprint        => 'F',
            rInstitution    => 'E',
            rState          => 'Z',
            rPostalCode     => 'AA',
            countryCode     => 'AB',
            rListPrice      => 'K',
            rPurchasePrice  => 'L',
            rDiscount       => 'M',
            rRevenue        => 'O',
            rTaxAmount      => 'Q',
            rFee            => 'N',
            rDuration       => 'U',
            rDeliveryMethod => 'C',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    # subtotal and unusedIsbn are used in isSaleRec
    my %fieldMap = (
        6 => {
            subtotal   => 'A',
            unusedIsbn => 'J',
            status     => 'R',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub rPurchasePriceCurrency { 'USD' }

sub rState {
    my $self  = shift;
    my $state = $self->_getByFieldName('rState');
    if ( $state =~ /(\w{2})/ ) {
        return $1;
    }
    return $state;
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( lc($type) eq 'digital' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unexpected product type: $type");
    }
}

sub rSales {
    my $self = shift;

    my $revenue = $self->revenue();
    if ( $revenue >= 0 ) {
        return 1;
    }
}

sub rReturns {
    my $self    = shift;
    my $revenue = $self->revenue();
    if ( $revenue < 0 ) {
        return 1;
    }
}

sub rRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');

    if ( !$revenue ) {
        $revenue = 0;
    }

    return $revenue;
}

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

    return $tax;
}

1;
