package BookPub::Import::Importer::PeterPal;
use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( isbn => 'EAN' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            purchaseType       => 'I',
            priceType          => 'P',
            rDistributor       => 'O',
            currencyCode       => 'Q',
            countryCode        => 'M',
            dateBegin          => 'K',
            isbn               => 'B',
            rTitle             => 'C',
            rAuthor            => 'D',
            rImprint           => 'E',
            rUnits             => 'L',
            rListPrice         => 'R',
            rListPriceCurrency => 'Q',
            rDiscount          => 'S',
            rUnitPrice         => 'T',
            rRevenue           => 'U',
            rInstitution       => 'O',
        },
        2 => {
            dateBegin              => 'O',
            purchaseType           => 'H',
            rFormat                => 'AD',
            priceType              => 'BR',
            rAuthor                => 'Y',
            rTitle                 => 'X',
            isbn                   => 'U',
            rImprint               => 'AC',
            countryCode            => 'AL',
            rReturns               => 'AG',
            rSales                 => 'AF',
            rUnits                 => 'AH',
            rUnitPrice             => 'AQ',
            rListPrice             => 'BQ',
            rListPriceCurrency     => 'AO',
            rPurchasePrice         => 'AM',
            rPurchasePriceCurrency => 'AO',
            rDiscount              => 'AP',
            rRevenue               => 'BC',
            currencyCode           => 'AO',
        },
    );

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

sub _unverifiedFieldMap {
    my $self               = shift;
    my %unverifiedFieldMap = (
        2 => {
            secondaryPriceType => 'AN',
            secondaryListPrice => 'AM',
        },
    );
    return $unverifiedFieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

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

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('purchaseType');
    if ( $purchaseType =~ /loan/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } else {
        $self->fail("Unrecognized purchase type: $purchaseType");
    }
}

sub countryCode {
    my $self = shift;

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

    return substr( $countryCode, 0, 2 );
}

1;
