package BookPub::Import::Importer::Gardners::Version13;

use strict;
use warnings;

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

use base 'BookPub::Import::Importer::Gardners::Version12';

sub _fieldMap {
    {
        dateBegin            => 'W',
        rPurchaseType        => 'G',
        rProductType         => 'B',
        rFormat              => 'B',
        rPriceType           => 'I1',    #Header
        rAuthor              => 'D',
        rTitle               => 'C',
        rIsbn13              => 'A',
        rInstitution         => 'X',
        rPostalCode          => 'Z',
        countryCode          => 'Y',
        rUnits               => 'O',
        rUnitPrice           => 'K',
        rListPrice           => 'I',
        rListPriceCurrency   => 'N',
        rDiscount            => 'L',
        rRevenue             => 'P',
        currencyCode         => 'N',
        rDuration            => 'H',
        rChannel             => 'V',
        rModel               => 'F',
        rTransactionCategory => 'E',
    };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my $hFields = $self->SUPER::_unverifiedFieldMap();
    $hFields->{vendorCountry} = 'Q';

    return $hFields;
}

sub countryCode {
    my $self = shift;

    my $country       = $self->_getByFieldName("countryCode")   || '';
    my $vendorCountry = $self->_getByFieldName("vendorCountry") || '';

    return $country || $vendorCountry || 'GB';
}

sub priceTypeQualifierID {
    my $self = shift;

    my $model = $self->_getByFieldName('rModel') || "";
    if ( $model =~ /^(?:library|academic)$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;    # Library
    }

    return;
}

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType') || '';
    if ( $productType =~ /EPUB/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $productType =~ /(ADOBE )?PDF/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $productType =~ /MP3/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

    $self->fail("Unable to determine product type from '$productType'.");
}

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

1;
