package BookPub::Import::Importer::BISG::Version3;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

# This version has no header or footer.  All of the data is repeated for each sale row.
#

sub _fieldMap {
    {
        # Detail
        rPriceType         => 'J',
        currencyCode       => 'I',
        countryCode        => 'AL',
        dateBegin          => 'O',
        rIsbn13            => 'U',
        rTitle             => 'X',
        rAuthor            => 'Y',
        rImprint           => 'AC',
        rUnits             => 'AH',
        rSales             => 'AF',
        rReturns           => 'AG',
        rFee               => 'AU',
        rListPrice         => 'AM',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BC',
    };
}

sub _headerIdentifier { ( rTitle => 'ProductTitle' ) }

sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode');

    my $oCountry = Common::Country->Get($code) if $code;
    return $oCountry->alpha2() if $oCountry;

    $self->fail("Unknown country code '$code'");
}

sub priceType {
    my $self = shift;
    my $type = lc( $self->rPriceType ) || return;

    if ( $type eq 'wholesale' ) {
        $type = 'rrp';
    }

    return $type;
}

sub rReturns {
    my $self = shift;
    my $returns = $self->SUPER::rReturns() || return;

    return $returns < 0 ? abs($returns) : 0;
}

sub rSales {
    my $self = shift;
    my $sales = $self->SUPER::rSales() || return;

    return $sales >= 0 ? $sales : 0;
}

1;
