package BookPub::Import::Importer::Copia::Version4;

use strict;

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

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

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

sub _headerIdentifier { ( rPriceType => 'Price type' ) }

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 $countryObj = Common::Country->Get($code) || $self->fail("Unknown country code '$code'");

    return $countryObj->alpha2();
}

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

    if ( $type == 1 ) {
        $type = 'rrp';
    } elsif ( $type == 2 ) {
        $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;
